简体   繁体   中英

Convert multiple rows of text into a Single row using Pandas

I have a dataframe like as shown below

df = pd.DataFrame({'text': ["Hi how are you","I am fine","I love you","I hate you"]})

I would like to convert all these individual rows into a single row

I tried the below but it is incorrect and doesn't work

df['text'].apply(' '.join).reset_index()

I expect my output to be like as shown below

在此处输入图片说明

How can I do this?

try via agg() , to_frame() and reset_index() :

out=(df.agg(' '.join)
        .to_frame('text')
        .reset_index(drop=True))

output of out:

    text
0   Hi how are you I am fine I love you I hate you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM