I applied groupby
on a DataFrame
and after that it converted the DataFrame
into pandas.core.groupby.groupby.DataFrameGroupBy
format.
How to convert pandas.core.groupby.groupby.DataFrameGroupBy
to regular DataFrame
or how can I access individual columns from pandas.core.groupby.groupby.DataFrameGroupBy
datatype?
It really depends on what you're trying to do.
Let's say this is my dataframe:
my_df = pandas.DataFrame([
{'Firm': 'A', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'A', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'B', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'B', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
])
grouped_df = my_df.groupby('Firm')
# you can iterate through your new groupby object like this
for firm, group in grouped_df:
print(firm, '\n', group)
#Output
A
Firm y1_bin y2_bin y3_bin
0 A binA binA binB
1 A binA binA binB
B
Firm y1_bin y2_bin y3_bin
2 B binA binA binB
3 B binA binA binB
# you can access individual values like this
for firm, group in grouped_df:
for _, row in group.iterrows():
print(row.firm, '\n', row.y1_bin)
#Output
A
binA
A
binA
B
binA
B
binA
More info: https://pandas.pydata.org/pandas-docs/stable/api.html#groupby
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.