【问题标题】:Is there a way to remove autotruncation from pandas dataframe?有没有办法从熊猫数据框中删除自动截断?
【发布时间】:2021-12-10 01:09:35
【问题描述】:

我正在尝试创建一个多索引数据框,其中包含所有可能的索引,即使是当前不包含值的索引。我希望将这些不存在的值设置为 0。为此,我使用了以下内容:

index_levels = ['Channel', 'Duration', 'Designation', 'Manufacturing Class']

grouped_df = df.groupby(by = index_levels)[['Total Purchases', 'Sales', 'Cost']].agg('sum')

grouped_df = grouped_df.reindex(pd.MultiIndex.from_product(grouped_df.index.levels), fill_value = 0)

预期结果:

 ___________________________________________________________________________________________ 
|Chan. | Duration   | Designation|    Manufact. |Total Purchases|  Sales      |   Cost      |
|______|____________|____________|______________|_______________|_____________|_____________|
|      | Month      | Special    |    Brand     |     0         |    0.00     |   0.00      |
|      |            |            |______________|_______________|_____________|_____________|
|      |            |            |    Generic   |     0         |    0.00     |   0.00      |
|Retail|            |____________|______________|_______________|_____________|_____________|
|      |            |Not Special |    Brand     |     756       | 15654.07    |   9498.23   |
|      |            |            |______________|_______________|_____________|_____________|
|      |            |            |    Generic   |     7896      |  98745.23   |    78953.56 |
|      |____________|____________|______________|_______________|_____________|_____________|
|      | Season     | Special    |    Brand     |     0         |  0.00       |    0.00     |
|      |            |            |______________|_______________|_____________|_____________|
|      |            |            |    Generic   |     0         |  0.00       |    0.00     |
|      |            |____________|______________|_______________|_____________|_____________|
|      |            |Not Special |    Brand     |     0         |  0.00       |    0.00     |
|      |            |            |______________|_______________|_____________|_____________|
|      |            |            |    Generic   |     0         |  0.00       |    0.00     |
|______|____________|____________|______________|_______________|_____________|_____________|

当至少一个索引级别包含一个值时,会产生此结果。但是,如果索引级别不包含任何值,则下面会产生以下结果。

___________________________________________________________________________________________ 
|Chan. | Duration   | Designation|    Manufact. |Total Purchases|  Sales      |   Cost      |
|______|____________|____________|______________|_______________|_____________|_____________|
|      | Month      | Not Special|    Brand     |     756       |  15654.07   |   9498.23   |
|      |            |            |______________|_______________|_____________|_____________|
|      |            |            |    Generic   |    7896       | 98745.23    |   78953.56  |
|Retail|____________|____________|______________|_______________|_____________|_____________|
|      | Season     |Not Special |    Brand     |       0       |    0.00     |     0.00    |
|      |            |            |______________|_______________|_____________|_____________|
|      |            |            |    Generic   |       0       |    0.00     |     0.00    |
|______|____________|____________|______________|_______________|_____________|_____________|

由于某种原因,这些值会继续被自动截断。如何修复索引,以便始终产生所需的结果,并且我始终可以可靠地使用这些索引进行计算,即使所述索引中没有值?

【问题讨论】:

  • 我无法重现这个(熊猫 1.3.2)。你能用一个可运行的例子来编辑你的问题吗?
  • 我已经编辑了示例以使其可重现。我相信现在您将能够验证我的示例@PeterLeimbigler

标签: python pandas multi-index


【解决方案1】:

您可以做的是预先构建所需的固定索引。例如,基于字典,其中键是用作组索引的列标签,值是所有可能的结果。

index_levels = {
    'Channel': ['Retails'], 
    'Duration': ['Month', 'Season'], 
    'Designation': ['Special', 'Not Special'], 
    'Manufacturing Class': ['Brand', 'Generic']
}

fixed_index = pd.MultiIndex.from_product(index_levels.values(), names=index_levels.keys())

那你就可以了

grouped_df = df.groupby(by=index_levels.keys())[['Total Purchases', 'Sales', 'Cost']].agg('sum')

grouped_df = grouped_df.reindex(fixed_index, fill_value=0)

【讨论】:

  • 我的索引级别基于另一个数据框的列。 Channel、Duration、Designation 和 Manufacturing Class 都是列。有没有办法根据这些列设置索引级别,然后在 fixed_index 中使用它们?
  • 我没有关注。 “根据这些列设置索引级别,然后在 fixed_index 中使用它们”是什么意思?
  • 抱歉,这些列来自包含单个索引的现有数据框。我正在尝试从四列创建一个多索引数据框:通道、持续时间、指定和制造类。我不知道是否可以使用这些列创建索引级别,或者我是否必须使用您在此处提供的内容创建这些索引级别。例如,我可以只获取索引级别并创建如下代码:index_levels = { df['Channel'], df['Duration'], df['Designation'], df['Manufacturing Class']}。这可能吗,还是我最好使用你的方法?
  • 谢谢,清楚多了!是的,你可以,我会用更好的解决方案更新我的答案。
  • 如果该列从一开始就没有包含该值,pandas 无法知道它应该包含在相应的索引级别中。每个索引列必须至少有一个级别值样本。包含不存在的值的唯一方法是事先手动创建所需的索引
猜你喜欢
  • 2021-11-30
  • 2019-06-26
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多