'pandas, creating dataframes based on tuple
I have a tuple that has data for several categories. Now I want to extract small dataframes from this tuple for each category based on a list I created. I want to give to each dataframe the name that I have in the list.
The big tuple I have is named 'out' and it contains data for all animals. The list of categories that I want to extract is like below
cat_list = ['cat', 'dog', 'snake']
I want to create separate dataframes for each category. I get each df like this
cat = out['cat']
dog = out['dog']
snake = out['snake']
But I want to do this in a for loop for the list I pass in. I tried something like this
for i in cat_list:
i = out[i]
But I get error:
SyntaxError: cannot assign to operator
Can someone please help me with this?
Solution 1:[1]
I guess what you are trying to create is a panda series. Code link
import pandas as pd
out = {
"cat": 1,
"dog": 2,
"snake": 3
}
cat_list = ['cat', 'dog', 'snake']
panda_series = {}
for i in cat_list:
panda_series[i] = pd.Series(out[i], name=i)
for key, series in panda_series.items():
print(series)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |