Isa*_*odo 5 python dataframe python-3.x pandas
我有一个来自 excel 的数据框,其中行中有几个 NaN。我想用另一个基线行替换值全部为 NaN 的行。
原始数据框是这样的:
Country Name Years tariff1_1 tariff1_2 tariff1_3
830 Hungary 2004 9.540313 6.287314 13.098201
831 Hungary 2005 9.540789 6.281724 13.124401
832 Hungary 2006 NaN NaN NaN
833 Hungary 2007 NaN NaN NaN
834 eu 2005 8.55 5.7 11.4
835 eu 2006 8.46 5.9 11.6
836 eu 2007 8.56 5.3 11.9
Run Code Online (Sandbox Code Playgroud)
因此,如果特定年份匈牙利的关税均为 NaN,则应根据确切年份,用欧盟数据替换该行。
理想的结果是:
Country Name Years tariff1_1 tariff1_2 tariff1_3
830 Hungary 2004 9.540313 6.287314 13.098201
831 Hungary 2005 9.540789 6.281724 13.124401
832 Hungary 2006 8.46 5.9 11.6
833 Hungary 2007 8.56 5.3 11.9
834 eu 2005 8.55 5.7 11.4
835 eu 2006 8.46 5.9 11.6
836 eu 2007 8.56 5.3 11.9
Run Code Online (Sandbox Code Playgroud)
我查看了特定行 ('Hungary',2006) 中 NaN 的类型,它变成了 'float64'。所以事实证明,输入类型不支持 ufunc 'isnan',并且输入不能是在我使用后,根据转换规则“安全”,安全地强制转换为任何支持的类型np.isnan。
所以我就采用了math.isnan。但它似乎没有检测到我的测试行中的NaN :
test=df.loc[(df['Country Name'] == 'Hungary') & (df['Years']== 2006)]
test.iloc[:,4]
Out[293]:
832 NaN
Name: tariff1_3, dtype: float64
math.isnan(any(test))
Out[294]:False
np.isnan(any(test))
Out[295]:ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Run Code Online (Sandbox Code Playgroud)
这是我的原创台词。
Eu=['Austria','Belgium','Curacao','Denmark','Finland','France','Germany']
for country in Eu:
for year in range(2001,2012)
if math.isnan(all(df.loc[(df['Country Name'] == country) & (df['Years'] == year)])):
df.loc[(df['Country Name'] == country) & (df['Years'] == year)]=df.loc[(df['Country Name'] == 'eu') & (df['Years'] == year)]
Run Code Online (Sandbox Code Playgroud)
谢谢 !
你可以试试 :
df.isnull().values.any()
Run Code Online (Sandbox Code Playgroud)
对于你的情况:
test.isnull().values.any()
Run Code Online (Sandbox Code Playgroud)