[英]Create a pandas dataframe column depending if a value is null or not
I have Data science-related project about a course students took in 2016. I have a column which shows at what dates did the students upgrade their course.我有一个关于学生在 2016 年参加的课程的数据科学相关项目。我有一个专栏,显示学生升级课程的日期。 If the course has not been upgraded the value is Null.如果课程尚未升级,则值为 Null。 What I want is to create a new data frame consisting of only this upgraded column consisting of "yes" or "no".我想要的是创建一个新的数据框,该数据框仅包含这个由“是”或“否”组成的升级列。 I have attempted the following code and it works, Except I get the following warning: "SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame."我尝试了以下代码并且它有效,除了我收到以下警告:“SettingWithCopyWarning:试图在 DataFrame 的切片副本上设置值。” I am putting a sample dataset, the code and the output I got.我正在放置一个示例数据集、代码和我得到的 output。 If someone can tell me a more efficient way with an explanation, It will be great.如果有人可以通过解释告诉我更有效的方法,那就太好了。
import pandas as pd
registration = pd.DataFrame({'upgraded':['2016-08-12 19:42:07+00:00', '2016-08-14 11:51:21+00:00',
'2016-07-22 17:24:59+00:00', None, None, '2016-07-12 10:33:02+00:00']})
upgraded_1 = registration[['upgraded']]
for i in range(len(upgraded_1['upgraded'])):
if pd.isnull(upgraded_1['upgraded'][i]):
upgraded_1['upgraded'][i] = "No"
else:
upgraded_1['upgraded'][i] = "Yes"
Output: Output:
upgraded_1
0 Yes
1 Yes
2 Yes
3 No
4 No
5 Yes
You can achieve this with the isna
method andnumpy.where
(think of it as numpy.if_then_else
).您可以使用isna
方法和numpy.where
来实现这一点(将其视为numpy.if_then_else
)。
>>> pd.DataFrame(np.where(registration.isna(), 'No', 'Yes'))
0
0 Yes
1 Yes
2 Yes
3 No
4 No
5 Yes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.