简体   繁体   中英

combining pandas data frame rows into a single vector

I have pandas data frame with one column.

I want to concatenate each value of column such that it turns into a single vector.

data_frame =            0
                0   [55,75,97]
                1 . [47,51,107]

result should be [55,75,97,47,51,107]

I tried np.vstack(), np.column_stack() etc., nothing worked out. Please help me

This will create a list from your column.

l = []
[l.extend(e) for e in df[0].values]
l # your list

Alternatively you can do:

sum(dataframe[0],[])

or

[item for row in dataframe[0] for item in row]

You can get all the columns in a separate list as below:

print (df['0'].tolist())
[[55,75,97], [47,51,107]]

So, we can combine them to a single list in this way.

print ([a for b in df[0].tolist() for a in b])
[[55,75,97,47,51,107]]

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