Home > OS >  Python - Read a CSV and Delete Last Comma of Last Row Value
Python - Read a CSV and Delete Last Comma of Last Row Value

Time:07-08

i need help please!

Have the next code to generate a .csv File:

sfdc_dataframe.to_csv('sfdc_data_demo.csv',index=False,header=True)

It is just 1 column, how could i get the last value of the column and delete the last comma in the value?

Example image of input data: https://i.stack.imgur.com/M5nVO.png

And the result that im try to make: https://i.stack.imgur.com/fEOXM.png

Anyone have an idea o tip?

From now, thanks!

CodePudding user response:

Once after reading csv file in dataframe(logic shared by you), you can use below logic which is specifically for last row of your specific column replace.

sfdc_dataframe['your_column_name'].iat[-1]=sfdc_dataframe['your_column_name'].iat[-1].str[:-1]

CodePudding user response:

Easy way

df = pd.read_csv("df_name.csv",  dtype=object)
df['column_name'] = df['column_name'].str[:-1]

CodePudding user response:

Updated answer below as it only required to change value of the last row.

val = sfdc_dataframe.iloc[-1, sfdc_dataframe.columns.get_loc('col')] 
sfdc_dataframe.iloc[-1, sfdc_dataframe.columns.get_loc('col')] = val[:-1]
  • Related