[英]How to see if column name exists in a dataframe, and if not create the column with a default value?
I want to add a Column_4
when Column_4
does not exists in the dataframe.当
Column_4
中不存在 Column_4 时,我想添加一个Column_4
。 How do I search for it, and if no match create the new column?我如何搜索它,如果没有匹配则创建新列?
Column_1 Column_2 Clolumn_3
1 4 7
2 5 8
3 6 9
Since Column_4
is not available, add Column_4
with value of 10
由于
Column_4
不可用,添加Column_4
值为10
Column_1 Column_2 Clolumn_3 Column_4
1 4 7 10
2 5 8 10
3 6 9 10
Check if the column name is in the columns list.检查列名是否在列列表中。 If not make it 10:
如果没有做到10:
if("Col_4" in df.columns):
print("Col_4 exists")
else:
df["Col_4"] = 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.