[英]Pandas Metadata Properties Not Passed to Groups of a Groupby Object
假设我有
df = pd.DataFrame({'A': [1,2,3], 'B': [1,2,1]})
df._metadata += "name"
df.name = "The Name"
groups = df.groupby(by="B")
for id, group in groups:
print(group.name)
print
function 将抛出AttributeError
。
现在,我需要以某种方式将元数据传递给每个单独的组。 如何才能做到这一点?
您应该使用此表示法 ( df['COLNAME'] = 'VALUE'
) 添加新列:
df = pd.DataFrame({'A': [1,2,3], 'B': [1,2,1]})
df['_metadata'] = "name"
df['name'] = "The Name"
groups = df.groupby(by="B")
for id, group in groups:
print(group.name)
Output:
0 The Name
2 The Name
Name: name, dtype: object
1 The Name
Name: name, dtype: object
您仍然可以从df
访问它,不是吗?
# ...
for id, group in groups:
print(df.name)
如果出于任何原因需要这样做,您也可以分配给每个组:
# ...
for id, group in groups:
group.name = df.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.