Ad
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
Ad
Answer
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())
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad