问题
this function display the crosstab table and visualize the result using matplotlib.
def event_mouhafaza(self,df):
df_event_mohafazat_crosstab = pd.crosstab(df['event_mohafaza'],df['event_type'])
print(df_event_mohafazat_crosstab)
data = []
#use for loop on every zoo name to create bar data
for x in df_event_mohafazat_crosstab.columns:
data.append(go.Bar(name=str(x), x=df_event_mohafazat_crosstab.index, y=df_event_mohafazat_crosstab[x]))
layout = go.Layout(
title = "Event type over Location",
xaxis=dict(
title="Mouhafazat"
),
yaxis=dict(
title="Number of people"
)
)
figure = go.Figure(data,layout=layout)
plotly.offline.plot(figure)
the below table is the result expected
event_type watch movie stay at home swimming camping meeting
event_mohafaza
loc1 0 65 0 254 13
loc2 60 0 125 19
loc3 518 3 705 87
loc4 721 11 318 147
loc5 0 103 0 214 17
this is the result in visualization
try to convert the code above to the code below where i take the arguments from the function and use it in the crosstab query.
def event_mouhafaza(self,df,items):
for item in items:
item1 = items[0]
item2 = items[1]
df_event_mohafazat_crosstab = pd.crosstab(item1,item2,index=item1)
print(df_event_mohafazat_crosstab)
data = []
#use for loop on every zoo name to create bar data
for x in df_event_mohafazat_crosstab.columns:
data.append(go.Bar(name=str(x), x=df_event_mohafazat_crosstab.index, y=df_event_mohafazat_crosstab[x]))
layout = go.Layout(
title = "Event type over Location",
xaxis=dict(
title="Mouhafazat"
),
yaxis=dict(
title="Number of people"
)
)
figure = go.Figure(data,layout=layout)
plotly.offline.plot(figure)
but the system crash and display this error:
df_event_mohafazat_crosstab = pd.crosstab(item1,item2,index=item1)
TypeError: crosstab() got multiple values for argument 'index'
来源:https://stackoverflow.com/questions/63899636/how-to-write-a-crosstab-query-in-pandas-and-python-with-visualization