pythonpandaslistcombiners

Combine cells from one column into one string


df = pd.read_csv(filename.csv)
corpus = df.corpus

How can I combine series of text strings (from one column) into a list?

from column 'corpus': row 1: Hail Mary.
row 2: Hi Bob.
row 3: Hey Sue.

into [Hail Mary. Hi Bob. Hey Sue.]

Looking for a list with len(list)=1.


Solution

  • If I understood you correctly:

    df = pd.read_csv(your_file)
    l = [' '.join(df['col'])]
    

    Input:

              col
    0  Hail Mary.
    1     Hi Bob.
    2    Hey Sue.
    

    Output:

    ['Hail Mary. Hi Bob. Hey Sue.']