2

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?

2
  • 2
    Could you add a small input/output example? Commented Dec 11, 2020 at 13:25
  • @DaniMesejo added sample input/output Commented Dec 11, 2020 at 13:31

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.