[英]Assigning new column based on other columns in Python
In Python I am trying to create a new column( degree
) within a dataframe
and to set its value based on if logic based on two other columns in the dataframe
(whether single rows of one or both these columns are null
values or not..).在 Python 中,我试图在dataframe
创建一个新列( degree
),并根据基于dataframe
其他两列的逻辑是否设置其值(这些列的单行或两列是否为null
值)。 )。 Per row it should assign to the new column the value of either one of these columns based on the presence of null
values in the column.每行它应该根据列中是否存在null
值将这些列中任一列的值分配给新列。
I have tried the below code, which gives me the following error message:我已经尝试了下面的代码,它给了我以下错误消息:
KeyError: 'degree'
The code is -代码是——
for i in basicdataframe.index:
if pd.isnull(basicdataframe['section_degree'][i]) and pd.isnull(basicdataframe['model_degree'][i]):
basicdataframe['degree'][i] = basicdataframe['model_degree'][i]
elif pd.notnull(basicdataframe['section_degree'][i]) and pd.isnull(basicdataframe['model_degree'][i]):
basicdataframe['degree'][i] = basicdataframe['section_degree'][i]
elif pd.isnull(basicdataframe['section_degree'][i]) and pd.notnull(basicdataframe['model_degree'][i]):
basicdataframe['degree'][i] = basicdataframe['model_degree'][i]
elif pd.notnull(basicdataframe['section_degree'][i]) and pd.notnull(basicdataframe['model_degree'][i]):
basicdataframe['degree'][i] = basicdataframe['model_degree'][i]
Does anybody know how to achieve this?有谁知道如何实现这一目标?
The error is because you are trying to assign values inside a column which does not exist yet.错误是因为您试图在尚不存在的列中分配值。
Since you are setting a new column as degree
, it makes sense if you add the column first with some default value.由于您将新列设置为degree
,因此如果您首先使用一些默认值添加该列是有意义的。
basicdataframe['degree'] = ''
This would set an empty string for all rows of the dataframe for this column.这将为该列的数据框的所有行设置一个空字符串。
After that, you can set the values.之后,您可以设置这些值。
PS Your code is likely to give you warnings about SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
. PS 您的代码可能会给您有关SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
警告SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
。
To fix that, you could take help from https://stackoverflow.com/a/20627316/1388513要解决此问题,您可以从https://stackoverflow.com/a/20627316/1388513 获得帮助
Let's say you have pandas Dataframe like this:假设您有这样的 Pandas Dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame(data={
"section_degree": [1, 2, np.nan, np.nan],
"model_degree": [np.nan, np.nan, np.nan, 3]
})
You can define function that will be applied to DataFrame:您可以定义将应用于 DataFrame 的函数:
def define_degree(x):
if pd.isnull(x["section_degree"]) and pd.isnull(x["model_degree"]):
return x["model_degree"]
elif pd.notnull(x['section_degree']) and pd.isnull(x['model_degree']):
return x["section_degree"]
elif pd.isnull(x['section_degree']) and pd.notnull(x['model_degree']):
return x["model_degree"]
elif pd.notnull(x['section_degree']) and pd.notnull(x['model_degree']):
return x["model_degree"]
df["degree"] = df.apply(define_degree, axis=1)
df
# output
section_degree model_degree degree
0 1.0 NaN 1.0
1 2.0 NaN 2.0
2 NaN NaN NaN
3 NaN 3.0 3.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.