There are multiple "\t" on my dataframe when I read the .csv table

Viewed 46

My dataframe was spread over multiple .csv files so I first used the glob module:

import glob
us_files = glob.glob("states*.csv")
us_list = []
for filenames in us_files:
    data = pd.read_csv(filenames)
    us_list.append(data)
us_census_df = pd.concat(us_list)

When I print the table is like:

\t"\tState\tTotalPop\tHispanic\tWhite\tBlack\tNative\tAsian\tPacific\tIncome\tGenderPop"
0   0\t"0\tAlabama\t4830620\t3.7516156462584975%\t...                                      
1   1\t"1\tAlaska\t733375\t5.909580838323351%\t60....                                      
2   2\t"2\tArizona\t6641928\t29.565921052631502%\t...                                      
3   3\t"3\tArkansas\t2958208\t6.215474452554738%\t...     

So I passed the dataframe into a .csv file and then I read the .csv file because with pd.read_csv I can choose the sep and delimiters:

us_census_df.to_csv('us_census.csv', sep ='\t')
us_census = pd.read_csv('us_census.csv', sep = '\t', delimiter = ',')   

But the table doesn't change....

1 Answers

try this with below code

import glob
import pandas as pd
us_files = glob.glob("states*.csv")
us_list = []
for filenames in us_files:
    us_list.append(pd.read_csv(filenames, sep='\t'))

us_census_df = pd.concat(us_list,ignore_index=True)

check this url

Related