Robert N
Robert N

Reputation: 3

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

I have the following code to generate a .csv File:

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

It is just one 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.sstatic.net/M5nVO.png

And the result that im try to make: https://i.sstatic.net/fEOXM.png

Anyone have an idea or tip?

Thanks!

Upvotes: 0

Views: 277

Answers (3)

user3503711
user3503711

Reputation: 2066

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]

Upvotes: 0

hasangzc
hasangzc

Reputation: 63

Easy way

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

Upvotes: 0

Manjari
Manjari

Reputation: 332

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]

Upvotes: 1

Related Questions