简体   繁体   English

根据值是否为 null 创建 pandas dataframe 列

[英]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.

相关问题 Pandas:如何根据每行包含 json 的列值创建新的 dataframe? - Pandas: how to create a new dataframe depending on a column value containing json for each row? dataframe 的列中存在的子集字符串,具体取决于另一列的值 - Pandas - Subsetting strings present in a column of a dataframe, depending on value of another column - Pandas Pandas dataframe 根据其他列的值划分列 - Pandas dataframe divide column depending on value of other column Pandas DataFrame:根据现有列的值检查将值写入列 - Pandas DataFrame: Writing values to column depending on a value check of existing column 如何根据多索引熊猫数据框中的行索引值创建列? - How to create a column depending on the row index values in a multiindex pandas dataframe? 如何根据索引将某个值分配到 Pandas 数据框中的新列中 - How to assign a certain value into a new column in a pandas dataframe depending on index 如何根据列值和不同的日期时间索引计算 pandas dataframe 的差异? - How to calculate difference in a pandas dataframe depending on column value and different datetimeindex? 在 Pandas DataFrame 中创建一个具有特定值的列 - Create a column with particular value in pandas DataFrame 在 Pandas 数据框中创建 value_counts 列 - Create column of value_counts in Pandas dataframe Pandas:在 dataframe 中创建列,并通过查看另一个 dataframe 为该列分配值 - Pandas: Create column in dataframe and assign value to the column by looking into another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM