我有下面的代码,它分析excel表格中的数据,生成每个水果的计数。我希望修改代码,这样我就有了另一个列,其中有每个水果的相对百分比,或者如果有一种方法,我可以编写一个代码来引用下面代码的输出,以提供所需的百分比
fruit = active["basket"].str.split(";").apply(pd.Series, 1).stack()
fruit = fruit.str.strip()
fruit = fruit.str.strip('#')
fruit.index = fruit.index.droplevel(-1)
fruit.name = "basket"
del active["basket"]
fruit = active.join(fruit)
fruit.to_excel(excel_writer, sheet_name = 'FRUIT_List', index = False)
fruit_counts = fruit.groupby(["basket"]).size().reset_index()
fruit_counts.rename(columns = {0: "Count"},inplace = True)
fruit_counts.to_excel(excel_writer, sheet_name = 'FRUIT_Counts', index = False)
print(fruit_counts)
输出:
Basket Count
Oranges 9
Mangoes 8
Watermelons 6
Sugarcanes 5
Grapes 3
将百分比列添加到“水果计数”数据框:
输出:
相关问题 更多 >
编程相关推荐