read a csv file in 3 columns

Question:

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.

Asked By: Elly

||

Answers:

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 )
Answered By: Blencer

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 -----------------------------------------------------------^
Answered By: Mureinik
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.