Mahmoud Al-Haroon
Mahmoud Al-Haroon

Reputation: 2449

How to remove the first column in data frame before to convert to csv

I have just a simple code that converts the txt file to csv, as now I want to delete the first column that's with no name as shown in the below figure:

enter image description here

and this is my simple code:

import pandas as pd
import os



hua_umts_dataf_rel_txt = 'umtsrelation_mnm.txt'
dataf_umts_txt_df = pd.read_csv(hua_umts_dataf_rel_txt, sep=';')
hua_umts_dataf_rel_df_column_index = list(dataf_umts_txt_df.columns)
dataf_umts_txt_df.reset_index(inplace=True)
dataf_umts_txt_df.drop(columns=dataf_umts_txt_df.columns[-1], inplace=True)
hua_umts_dataf_rel_df_column_index = dict(zip(list(dataf_umts_txt_df.columns), hua_umts_dataf_rel_df_column_index))
dataf_umts_txt_df.rename(columns=hua_umts_dataf_rel_df_column_index, inplace=True)
#dataf_umts_txt_df.__delitem__(dataf_umts_txt_df.columns[0])
dataf_umts_txt_df.to_csv('umtsrelation_mnm.csv', sep=';', encoding='utf-8')

print(dataf_umts_txt_df)


Upvotes: 0

Views: 2807

Answers (2)

Cohan
Cohan

Reputation: 4564

The pandas.to_csv() function has a parameter index. It defaults to True and prints the row names (or your dataframe index) to the csv.

index : bool, default True
    Write row names (index).

Set index to False

dataf_umts_txt_df.to_csv('umtsrelation_mnm.csv', sep=';', encoding='utf-8', index=False)

Upvotes: 1

Mike Müller
Mike Müller

Reputation: 85492

This first column is the index. Export it without the index with index=False:

dataf_umts_txt_df.to_csv('umtsrelation_mnm.csv', sep=';', index=False, encoding='utf-8')

Upvotes: 3

Related Questions