[英]create multiple df in loop based on condition
I need to create multiple dataframes from slices of a bigger dataframe in pandas based on a condition.我需要根据条件从 pandas 中更大的 dataframe 切片创建多个数据帧。 The different dataframes have to be named on the basis of some row values of the big dataframe.必须根据大 dataframe 的某些行值来命名不同的数据帧。
This is the big dataframe:这是大号 dataframe:
Id Valore
ID554 53.0
ID554 43.0
ID522 42.0
ID522 32.0
ID566 26.0
therefore the different dataframes have to be named ID554, ID522, ID566 and so on.因此,不同的数据帧必须命名为 ID554、ID522、ID566 等。 I have tried this:我试过这个:
id=df['Id'].unique()
for a in id:
a=df.loc[(df['ID']==a)]
it does not work though..虽然它不起作用..
You can use .groupby
:您可以使用.groupby
:
dataframes = {}
for name, g in df.groupby("Id"):
dataframes[name] = g
# print the data:
for k, v in dataframes.items():
print("Name:", k)
print("-" * 80)
print(v)
print()
Prints:印刷:
Name: ID522
--------------------------------------------------------------------------------
Id Valore
2 ID522 42.0
3 ID522 32.0
Name: ID554
--------------------------------------------------------------------------------
Id Valore
0 ID554 53.0
1 ID554 43.0
Name: ID566
--------------------------------------------------------------------------------
Id Valore
4 ID566 26.0
Use exec
使用exec
df = pd.DataFrame({"Id":["ID554","ID554","ID522","ID522","ID566"],"Valore":[53,43,42,32,26]})
for i in df.Id.unique():
exec(f"{i} = df[df.Id == i]")
Following code will make different dataframes and save it in different variables with the name of its ID.以下代码将创建不同的数据框并将其保存在不同的变量中,并以其 ID 的名称。
OUTPUTS:输出:
print(ID522)
Id Valore
2 ID522 42
3 ID522 32
print(ID554)
Id Valore
0 ID554 53
1 ID554 43
print(ID566)
Id Valore
4 ID566 26
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.