我看过到处都尝试过.loc
.apply
并使用lambda
,但是我仍然无法弄清楚。
我在熊猫数据框中有UCI国会投票数据集,而每个民主党或共和党议员的1至16票都缺少一些票。
因此,我在名为abs
的每个投票列之后插入了16列。
如果相应的投票栏为NaN,我希望每个abs栏都为1。
我在此站点上阅读的上述方法中没有一个对我有用。
所以我下面的代码段也行不通,但可能会提示我当前使用基本迭代Python语法的尝试。
for i in range(16):
for j in range(len(cvotes['v1'])):
if cvotes['v{}'.format(i+1)][j] == np.nan:
cvotes['abs{}'.format(i+1)][j] = 1
else:
cvotes['abs{}'.format(i+1)][j] = 0
有什么建议吗?
上面所述,当投票值为NaN或1时,我目前获得1分的绝对值。
编辑:
我看到了给出的答案,所以只用一列就尝试了
cols = ['v1']
for col in cols:
cvotes = cvotes.join(cvotes[col].add_prefix('abs').isna().
astype(int))
但这给了我一个错误:
ValueError: columns overlap but no suffix specified: Index(['v1'], dtype='object')
我的dtypes是:
party object
v1 float64
v2 float64
v3 float64
v4 float64
v5 float64
v6 float64
v7 float64
v8 float64
v9 float64
v10 float64
v11 float64
v12 float64
v13 float64
v14 float64
v15 float64
v16 float64
abs1 int64
abs2 int64
abs3 int64
abs4 int64
abs5 int64
abs6 int64
abs7 int64
abs8 int64
abs9 int64
abs10 int64
abs11 int64
abs12 int64
abs13 int64
abs14 int64
abs15 int64
abs16 int64
dtype: object
答案 0 :(得分:1)
让我们只用join
做add_prefix
col=[c1,c2...]
s=pd.DataFrame(df[col].values.tolist(),index=df.index)
s.columns=s.columns+1
df=df.join(s.add_prefix('abs').isna().astype(int))