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
Pandas NaN introduced by pivot_table
提问by Georg Heiler
I have a table containing some countries and their KPI from the world-banks API. this looks like . As you can see no nan values are present.
我有一个表格,其中包含来自世界银行 API 的一些国家及其 KPI。这看起来像。如您所见,不存在 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. TUERKEI
this works just fine:
但是,我需要旋转此表以将 int 转换为正确的形状以进行分析。Apd.pivot_table(countryKPI, index=['germanCName'], columns=['indicator.id'])
对于某些例如TUERKEI
这工作得很好:
But for most of the countries strange nan values are introduced. How can I prevent this?
回答by jezrael
I think the best way to understand pivoting
is 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 NaN
to 0
add 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 返回以下数据帧:
Now I want to replace the Nan with 0, I will apply the fillna() method on the returned data frame from pivot method