根据条件,将熊猫DataFrame列从字符串转换为Int

时间:2021-04-22 22:58:47

I have a dataframe that looks like

我有一个看起来像的dataframe。

df

df

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

I want to convert the "viz" column to 0 and 1, based on a conditional. I've tried:

我想根据条件将“viz”列转换为0和1。我试过了:

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

but I get:

但我得到:

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

1 个解决方案

#1


8  

You're trying to compare a scalar with the entire series which raise the ValueError you saw. A simple method would be to cast the boolean series to int:

你试图将一个标量与整个级数进行比较这会增加你看到的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

You can also use np.where:

你也可以使用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

Output from the boolean comparison:

布尔比较输出:

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

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

And then casting to int:

然后转到int:

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

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

#1


8  

You're trying to compare a scalar with the entire series which raise the ValueError you saw. A simple method would be to cast the boolean series to int:

你试图将一个标量与整个级数进行比较这会增加你看到的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

You can also use np.where:

你也可以使用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

Output from the boolean comparison:

布尔比较输出:

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

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

And then casting to int:

然后转到int:

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

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