基于条件将Pandas DataFrame列从String转换为Int

时间:2015-08-03 14:44:55

标签: python pandas dataframe

我的数据框看起来像

DF

viz  a1_count  a1_mean     a1_std
n         3        2   0.816497
y         0      NaN        NaN 
n         2       51  50.000000

我想转换" viz"列为0和1,基于条件。我试过了:

df['viz'] = 0 if df['viz'] == "n" else 1

但我明白了:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

2 个答案:

答案 0 :(得分:17)

您试图将标量与提升ValueError的整个系列进行比较。一个简单的方法是将布尔系列转换为int

In [84]:
df['viz'] = (df['viz'] !='n').astype(int)
df

Out[84]:
   viz  a1_count  a1_mean     a1_std
0    0         3        2   0.816497
1    1         0      NaN        NaN
2    0         2       51  50.000000

您还可以使用np.where

In [86]:
df['viz'] = np.where(df['viz'] == 'n', 0, 1)
df

Out[86]:
   viz  a1_count  a1_mean     a1_std
0    0         3        2   0.816497
1    1         0      NaN        NaN
2    0         2       51  50.000000

布尔比较的输出:

In [89]:
df['viz'] !='n'

Out[89]:
0    False
1     True
2    False
Name: viz, dtype: bool

然后转向int

In [90]:
(df['viz'] !='n').astype(int)

Out[90]:
0    0
1    1
2    0
Name: viz, dtype: int32

答案 1 :(得分:0)

来自上面@TMWP 的评论:

pd.to_numeric(myDF['myDFCell'], errors='coerce')

它就像一个魅力,是一种快速简单的单衬