简体   繁体   中英

Pandas get data from a list of column names

I have a pandas dataframe: df and list of column names: columns like so:

df = pd.DataFrame({
    'A': ['b','b','c','d'],
    'C': ['b1','b2','c1','d2'],
    'B': list(range(4))})

columns = ['A','B']

Now I want to get all the data from these columns of the dataframe in one single series like so:

b
0
b
1
c
2
d
4

This is what I tried:

srs = pd.Series()
srs.append(df[column].values for column in columns)

But it is throwing this error:

TypeError: cannot concatenate object of type '<class 'generator'>'; only Series and DataFrame objs are valid

How can I fix this issue?

I think you can use numpy.ravel :

srs = pd.Series(np.ravel(df[columns]))
print (srs)
0    b
1    0
2    b
3    1
4    c
5    2
6    d
7    3
dtype: object

Or DataFrame.stack with Series.reset_index and drop=True :

srs = df[columns].stack().reset_index(drop=True)

If order should be changed is possible use DataFrame.melt :

srs = df[columns].melt()['value']
print (srs)
0    b
1    b
2    c
3    d
4    0
5    1
6    2
7    3
Name: value, dtype: object

You could do:

from itertools import chain

import pandas as pd
df = pd.DataFrame({
    'A': ['b','b','c','d'],
    'C': ['b1','b2','c1','d2'],
    'B': list(range(4))})

columns = ['A','B']

res = pd.Series(chain.from_iterable(df[columns].to_numpy()))
print(res)

Output

0    b
1    0
2    b
3    1
4    c
5    2
6    d
7    3
dtype: object

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