【问题标题】:Why NaN in pivot table?为什么在数据透视表中使用 NaN?
【发布时间】:2019-10-30 11:37:39
【问题描述】:

我已经使用 df = df.fillna(0) 从 df 中删除了所有 NaN。

在我使用

创建数据透视表之后
pd.pivot_table(df, index='Source', columns='Customer Location', values='Total billed £')

我仍然会再次获得NaN 数据作为输出。

有人可以解释一下为什么以及如何防止这种输出以及为什么会发生这种情况吗?

【问题讨论】:

    标签: python pandas dataframe pivot-table


    【解决方案1】:

    由于您的输入数据,它会将一列转换为索引,并将另一列的值转换为列。这些的交集是聚合值。 但如果输入数据中不存在某些组合,则会导致数据丢失(NaN)。

    df = pd.DataFrame({
            'Source':list('abcdef'),
             'Total billed £':[5,3,6,9,2,4],
             'Customer Location':list('adfbbb')
    })
    
    print (df)
      Source  Total billed £ Customer Location
    0      a               5                 a
    1      b               3                 d
    2      c               6                 f
    3      d               9                 b
    4      e               2                 b
    5      f               4                 b
    
    #e.g because `Source=a` and `Customer Location=b` not exist in source then NaN in output
    print (pd.pivot_table(df,index='Source', columns='Customer Location',values='Total billed £'))
    Customer Location    a    b    d    f
    Source                               
    a                  5.0  NaN  NaN  NaN
    b                  NaN  NaN  3.0  NaN
    c                  NaN  NaN  NaN  6.0
    d                  NaN  9.0  NaN  NaN
    e                  NaN  2.0  NaN  NaN
    f                  NaN  4.0  NaN  NaN
    

    此外,here'sreshaping data 进行了很好的阅读。

    【讨论】:

      【解决方案2】:

      原因很简单,您的数据中缺少一对(索引,列)值,例如:

      df = pd.DataFrame({"Source": ["foo", "bar", "bar", "bar"],
                         "Customer Location": ["one", "one", "two", "two", ],
                         "Total billed £": [10, 20, 30, 40]})
      
      print(df)
      

      设置

        Source Customer Location  Total billed £
      0    foo               one              10
      1    bar               one              20
      2    bar               two              30
      3    bar               two              40
      

      如您所见,您的数据中没有 ('foo', 'two') 对,所以当您这样做时:

      result = pd.pivot_table(df, index='Source', columns='Customer Location', values='Total billed £')
      print(result)
      

      输出

      Customer Location   one   two
      Source                       
      bar                20.0  35.0
      foo                10.0   NaN
      

      要解决此问题,请使用 fill_value 参数提供默认值:

      result = pd.pivot_table(df, index='Source', columns='Customer Location', values='Total billed £', fill_value=0)
      

      输出

      Customer Location  one  two
      Source                     
      bar                 20   35
      foo                 10    0
      

      【讨论】:

      • @jezrael 如果您认为是重复的,我很乐意删除我的答案。但是从您的措辞中,我并不清楚,至少问题在于缺少 (index, column) 对
      • yop,同意,不清楚 OP 是否需要将 NaN 替换为 0 或需要解释问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2013-12-18
      相关资源
      最近更新 更多