2016-03-16 38 views
1

na.rm我有以下數據:計算rowmeans忽略娜的大熊貓,像R中

a = pd.Series([1, 2, "NA"]) 
b = pd.Series(["NA", 2, 3]) 
df = pd.concat([a, b], axis=1) 
#  0 1 
# 0 1 NA 
# 1 2 2 
# 2 NA 3 

現在,我想計算rowmeans像R中與na.rm=T

c.mean(skipna=True, axis=0) 
# Series([], dtype: float64) 

我期待:

# 0 1 # 1/1 
# 1 2 # (2+2)/2 
# 2 3 # 3/1 

如何實現這一目標?

回答

1

您有混合dtypes由於STR「NA」的存在,你需要先轉換爲數值類型:

In [118]: 
df.apply(lambda x: pd.to_numeric(x, errors='force')).mean(axis=1) 

Out[118]: 
0 1 
1 2 
2 3 
dtype: float64 

如果你的原始數據是真實的NaN然後它按預期工作:

In [119]: 
a = pd.Series([1, 2, np.NaN]) 
b = pd.Series([np.NaN, 2, 3]) 
df = pd.concat([a, b], axis=1) 
df.mean(skipna=True, axis=1) 

Out[119]: 
0 1 
1 2 
2 3 
dtype: float64 
+1

僅供參考:當只傳遞kwargs到''.apply''時,你可以直接執行:''df.apply(pd.to_numeric,errors ='force')。mean(axis = 1)'' – Jeff

+0

@Jeff感謝您的提示,當函數將args作爲默認用法時,我傾向於使用'lambda's – EdChum