[英]Pandas: how to operate with each pivottable value?
This is the continuation of this . 这是延续此 。 Let's say I have code that does this table I want 假设我有执行此表的代码
df.groupby('A')['B'].value_counts().unstack().stack(dropna=False
).reset_index(name="Count").set_index(['A', 'B'])
|----|----|-------|
| A | B | Count |
| a1 | b1 | 1 |
| | b2 | 1 |
| | b3 | NaN |
| a2 | b1 | 1 |
| | b2 | NaN |
| | b3 | 1 |
The problem is that there is the case where B column might have multivariate values, eg many unique values. 问题是,在某些情况下, B列可能具有多元值,例如许多唯一值。 So the groupping A column values are a bit far away :) Eventually, this all should be stored in some Excel format file df.to_excel()
. 因此,分组A列的值有点远:)最终,所有这些都应存储在某些Excel格式的文件df.to_excel()
。 The solution was proposed to generate such as Excel files per A values. 建议该解决方案生成每个A值的Excel文件。 Eg instead of groupped.xlsx where you have all this pivot table in once, to have A_a1.xlsx, A_a2.xlsx files. 例如,而不是groupped.xlsx ,其中一次拥有所有此数据透视表,从而拥有A_a1.xlsx,A_a2.xlsx文件。
Question: how do you do it? 问题:您如何做?
I have some options in mind like to get the list of unique A values and just do something like df_loc = df.loc[df['A'] == 'a1']
, but maybe there is more cool way? 我有一些选择,例如获取唯一的A值列表,然后执行df_loc = df.loc[df['A'] == 'a1']
,但也许还有更酷的方法吗?
If I understand correctly you're looking for an individual excel file for each A value? 如果我理解正确,您正在为每个A值寻找一个单独的excel文件? If so, the following should work: 如果是这样,则应执行以下操作:
for i in df.a.unique(): df[df['a'] == i].to_excel(path+_i)
You can tweak the path for your needs but this is a pretty easy way to do what you're looking for. 您可以根据自己的需要调整路径,但这是完成所需工作的简单方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.