Search code examples
pythonpandas

concatenate all strings in the dataframe column


I want to convert all the strings in a dataframe column as a single empty string and then convert it in to list of words:

import pandas as pd
df = pd.DataFrame({'read': ["Red", "is", "my", "favorite", "color"]})
print(df)
    read
0   Red
1   is
2   my
3   favorite
4   color

I tried to join strings but I don't know how to add space.

string = ""
for i,j in df.iterrows():
    string += j["read"]

Output:

'Redismyfavoritecolor' 

Required Output:

"Red is my favorite color"

Solution

  • Use join with whitespace:

    out = ' '.join(df["read"])
    print (out)
    Red is my favorite color