read a csv file in 3 columns

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:

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.

>Solution :

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 -----------------------------------------------------------^

Leave a Reply