简体   繁体   English

如何通过python中的键在循环中命名pandas数据帧?

[英]How to name pandas dataframes in a loop by their key in python?

I would like to create data-frames in a loop, but name each data-frame using a key as not to overwrite each datafranme in the loop. 我想在循环中创建数据帧,但是使用键命名每个数据帧,而不是覆盖循环中的每个数据帧。

Here is a simplified version of my data-frame: 这是我的数据框的简化版本:

ID  Field  Value
1     A     1.1
2     A     1.2
3     A     2.4
4     B     1.7
5     B     4.3
6     C     2.2

So in this case I would like to end up with 3 data frames named A, B and C so this is what I tired: 所以在这种情况下,我想最终得到3个名为A,B和C的数据框,所以这就是我累了:

df2= df.groupby(['Field'])
for key, group in df2:
   key = group.reset_index()

But ofcourse the name 'key' gets overwritten with each sucessive loop. 但是当然,每个过度循环都会覆盖“密钥”这个名称。 How can I name each dataframe in the loop by its key? 如何通过其键为循环中的每个数据框命名?

I would lalso like to create a list of the created dataframes as to keep track of them. 我还想创建一个创建的数据帧列表,以便跟踪它们。

You want to store your objects in a dict: 您想将对象存储在dict中:

df_dict = {}
for key, group in df2:
   df_dict[key] = group.reset_index()

使用字典理解,更简洁的解决方案如下:

df_new = {field: df.loc[df.Field == field, :] for field in df.Field.unique()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM