[英]Pandas: creating a new column with “Yes” in case a pre-existing column contains some value and “No” if the value of the column is ' '
I am trying to add a column in my "df" that contains "Yes" in case a pre-existing column contains some value and that says "No" if the value of the column is ' ' (note the space between ' ').我正在尝试在我的“df”中添加一个包含“是”的列,以防预先存在的列包含一些值,并且如果列的值为“”,则显示“否”(注意“”之间的空格) .
Here's an example:这是一个例子:
my_dict = {'Products': {0: 0, 1: 1, 2: 2}, 'Prices': {0: ' ', 1: ' ', 2: 'C'}}
my_df = pd.DataFrame(my_dict)
my_df['Direct debit'] = my_df['Category'].apply(lambda x: "Yes" if not "' '" else "NO")
returns:返回:
The "output" is NO in all cases, but it should say YES where my_df['Category'] has any value.在所有情况下,“输出”都是“否”,但它应该在 my_df['Category'] 有任何值的地方说“是”。
What should I fix in my code?我应该在我的代码中修复什么?
Use np.where
:使用
np.where
:
import numpy as np
my_df['Direct debit'] = np.where(my_df['Category'] != '', 'Yes', 'No')
By the way, keep in mind that ' '
, ''
, are different顺便说一句,请记住
' '
、 ''
是不同的
Try this.尝试这个。 It is much faster than lambda functions on large datasets:
它比大型数据集上的 lambda 函数快得多:
my_df['Direct debit'] = np.where(my_df['Category'] != '', 'YES', 'NO')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.