pythondumb
pythondumb

Reputation: 1247

Converting a specific column data in .csv to text using Python pandas

I have a .csv file like below where all the contents are text

col1             Col2
My name          Arghya
The Big Apple    Fruit

I am able to read this csv using pd.read_csv(index_col=False, header=None). How do I combine all the three rows in Col1 into a list separated by a full stop.

Upvotes: 1

Views: 1527

Answers (1)

jezrael
jezrael

Reputation: 863256

If need convert column values to list:

print (df.Col1.tolist())
#alternative solution
#print (list(df.Col1))
['This is Apple', 'I am in Mumbai', 'I like rainy day']

And then join values in list - output is string:

a = '.'.join(df.Col1.tolist())
print (a)
This is Apple.I am in Mumbai.I like rainy day

print (df)
                  0      1
0              Col1   Col2
1     This is Apple  Fruit
2    I am in Mumbai  Great
3  I like rainy day  Flood


print (list(df.loc[:, 0]))
#alternative
#print (list(df[0]))
['Col1', 'This is Apple', 'I am in Mumbai', 'I like rainy day']

Upvotes: 2

Related Questions