Suppose I have
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)
The print
function will throw an AttributeError
.
Now, I would need to somehow pass the metadata to each individual group. How can this be done?
You should use this notation ( df['COLNAME'] = 'VALUE'
) to add new columns:
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
You could still access it from df
, no?
# ...
for id, group in groups:
print(df.name)
You could also then assign to each group if you need to do that for whatever reason:
# ...
for id, group in groups:
group.name = df.name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.