'Python Pandas, transposing DataFrameGroupBy object (Generating transpose of pandas grouped data)
How can I generate a transpose of pandas grouped data. That is DataFrameGroupBy object (DataFrame.T is not working for grouped data. Which is a DataFrameGroupBy object)
#Grouping data
def group_by(file_=None,column_=None):
"""Return data of the file grouped by column specified as the parameter"""
data=load_data(file_)
return data.groupby(column_)
#Transpose table to plot
def data_transpose(data_,grouped_=False,column_=None):
"""Create a transpose of the dataframe. If transposing grouped data, set grouped_ to True and must specify column. Enter file, followed by boolean value then column name to group by to transpose grouped data"""
#Each column corresponds to each county
if grouped_:
#must specify column
grp_data=group_by(data_,column_)
return grp_data.T
data=load_data(data_)
return data.T```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|