agenis
agenis

Reputation: 8387

create a list of pd.Series from a panda DataFrame

I have a DataFrame with a lot of columns, and a list of the column names I want to pass to an algorithm. The algorithm requires that I pass in argument not a dataframe but a list where each element is one of my pd.Series columns.

I think this question might already have been answered but I can' find... If there a function or method to achieve this transformation data.Frame => list of pd.Series?

Some code to show my desired output with 2 columns but my use case if with dozens of them, so I can't write it manually:

mydf = pd.DataFrame.from_dict({'a': {0: 1, 1: 2}, 'b': {0: 3, 1: 3}, 'c': {0: 10, 1: 3}})
my_list_of_columns=["a", "b"]
desired_output = [mydf.a, mydf.b]

Thanks for the help

Upvotes: 1

Views: 67

Answers (2)

Uwe Ziegenhagen
Uwe Ziegenhagen

Reputation: 694

Something like this?

import pandas as pd

mydf = pd.DataFrame.from_dict({'a': {0: 1, 1: 2}, 'b': {0: 3, 1: 3}, 'c': {0: 10, 1: 3}})

desired_output = []
l = list(mydf)

for i in l:
    desired_output.append(list(eval('mydf.' + i)))


print(desired_output)

Upvotes: 1

jezrael
jezrael

Reputation: 863751

Use list comprehension:

desired_output = [mydf[x] for x in my_list_of_columns]
print (desired_output)
[0    1
1    2
Name: a, dtype: int64, 0    3
1    3
Name: b, dtype: int64]

Or convert to to_dict with to_dict(orient='series') and get values of dict, but ordering should be changed in python under 3.6:

desired_output = list(mydf[my_list_of_columns].to_dict(orient='series').values())

Upvotes: 2

Related Questions