我有一个数据帧,如下所示:
Year Sex Area Count
2015 W Dhaka 6
2015 M Dhaka 3
2015 W Khulna 1
2015 M Khulna 8
2014 M Dhaka 13
2014 W Dhaka 20
2014 M Khulna 9
2014 W Khulna 6
2013 W Dhaka 11
2013 M Dhaka 2
2013 W Khulna 8
2013 M Khulna 5
2012 M Dhaka 12
2012 W Dhaka 4
2012 W Khulna 7
2012 M Khulna 1
我的目的是显示一些数据根据用户输入.as例如,用户可以从下拉列表中输入一个城市,然后根据该城市的数据构建数据帧。你知道吗
这里的问题是,假设用户先选择“达卡”,然后显示数据正确。之后当用户输入“Khulna”时,dataframe只是将Khulna数据附加到前面的dataframe数据中。你知道吗
但我现在只想保留库尔纳的数据。你知道吗
def update(event):
chosen_option = var1.get()
df=df_global #df_global is the dataframe with main read file
grp_city=df
#select data according to variables year and city
df=df.loc[df['"Year"']<=end]
df=df.loc[df['"Year"']>=start]
lst1=list(grp_city['Area'].unique())
df=df.loc[df['Area']==chosen_option]
agg_df = df.groupby(['"Year"','Sex']).sum()
agg_df.reset_index(inplace=True)
piv_df = agg_df.pivot(index='"Year"', columns='Sex', values='Count')
fig = plt.figure(1)
ax1 = fig.add_subplot(211)
piv_df.plot.bar(stacked=True,ax=ax1)
canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()
plot_widget.grid(row=0, column=0)
有没有办法每次都清除整个数据帧?你知道吗
通过替换fig=plt.图(1) 带图=plt.图() 通过全局读取csv并每次过滤该数据帧,也解决了数据帧问题。你知道吗
相关问题 更多 >
编程相关推荐