pandas 由pivot_table引入的Pandas NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39632277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:04:02  来源:igfitidea点击:

Pandas NaN introduced by pivot_table

pythonpandaspivotpivot-tablenan

提问by Georg Heiler

I have a table containing some countries and their KPI from the world-banks API. this looks like no nan values present. As you can see no nan values are present.

我有一个表格,其中包含来自世界银行 API 的一些国家及其 KPI。这看起来像不存在 nan 值。如您所见,不存在 nan 值。

However, I need to pivot this table to bring int into the right shape for analysis. A pd.pivot_table(countryKPI, index=['germanCName'], columns=['indicator.id'])For some e.g. TUERKEIthis works just fine:

但是,我需要旋转此表以将 int 转换为正确的形状以进行分析。Apd.pivot_table(countryKPI, index=['germanCName'], columns=['indicator.id'])对于某些例如TUERKEI这工作得很好:

for turkey it worksBut for most of the countries strange nan values are introduced. How can I prevent this?

对于火鸡它有效但是对于大多数国家来说,引入了奇怪的 nan 值。我怎样才能防止这种情况?

strange nan values

奇怪的 nan 值

回答by jezrael

I think the best way to understand pivotingis to apply it to a small sample:

我认为最好的理解方法pivoting是将其应用于小样本:

import pandas as pd
import numpy as np

countryKPI = pd.DataFrame({'germanCName':['a','a','b','c','c'],
                           'indicator.id':['z','x','z','y','m'],
                           'value':[7,8,9,7,8]})

print (countryKPI)
  germanCName indicator.id  value
0           a            z      7
1           a            x      8
2           b            z      9
3           c            y      7
4           c            m      8

print (pd.pivot_table(countryKPI, index=['germanCName'], columns=['indicator.id']))
             value               
indicator.id     m    x    y    z
germanCName                      
a              NaN  8.0  NaN  7.0
b              NaN  NaN  NaN  9.0
c              8.0  NaN  7.0  NaN

If need replace NaNto 0add parameter fill_value:

如果需要更换NaN,以0添加参数fill_value

print (countryKPI.pivot_table(index='germanCName', 
                              columns='indicator.id', 
                              values='value', 
                              fill_value=0))
indicator.id  m  x  y  z
germanCName             
a             0  8  0  7
b             0  0  0  9
c             8  0  7  0

回答by Arpan Saini

As per documentations:

根据文件:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html

pivot method returns:reshaped DataFrame.

枢轴方法返回:重塑数据帧。

Now you can replace the na values with any desired values, using fillna method.

现在,您可以使用 fillna 方法将 na 值替换为任何所需的值。

FOR EXAMPLE:

例如:

MY PIVOT RETURNS THE BELOW dataFrame:

我的 PIVOT 返回以下数据帧:

PIVOT RETURN DATA TYPENow I want to replace the Nan with 0, I will apply the fillna() method on the returned data frame from pivot method

枢轴返回数据类型现在我想用0替换Nan,我将对从pivot方法返回的数据帧应用fillna()方法

DATA FRAME RETURN AFTER REPLACING Nan values with 0

将 Nan 值替换为 0 后的数据帧返回