Home > OS >  read a csv file in 3 columns
read a csv file in 3 columns

Time:01-14

I want to read a csv file with 3 columns: "source","target","genre_ids" with python

df = pd.read_csv('edges1.csv',encoding="ISO-8859-1",  delimiter=';;', header=None,skiprows=1, names=columns,engine="python",index_col=False )
data = pd.concat([df.iloc[:,0].str.split(',', expand=True).rename(columns:=['source','target','genre_ids']), axis==1])

I want to get:

source          target    genre_ids
apple           green      21
strawberry       red       23
son on

edge1.csv contains:

source,target,genre_ids
apple,green,21
strawberry,red,23
and so on

when I read the edges1.csv file I have data on only one column. To separate the columns I found the concat method online which splits the columns, but it doesn't work.

CodePudding user response:

From the sample data, it seems the delimiter is ,, but in your code you're explicitly setting it to ;;. Using the correct delimiter should solve the problem:

df = pd.read_csv('edges1.csv', encoding="ISO-8859-1",  delimiter=',', header=None, skiprows=1, names=columns,e ngine="python", index_col=False )
# Here -----------------------------------------------------------^

CodePudding user response:

Your read_csv call uses delimiter=';;'. Remove it to use the default delimiter, which is ','.

df = pd.read_csv('edges1.csv',encoding="ISO-8859-1", header=None, skiprows=1, names=columns,engine="python", index_col=False )
  • Related