繁体   English   中英

如何查看 dataframe 中是否存在列名,如果不存在则使用默认值创建列?

[英]How to see if column name exists in a dataframe, and if not create the column with a default value?

Column_4中不存在 Column_4 时,我想添加一个Column_4 我如何搜索它,如果没有匹配则创建新列?

Column_1   Column_2  Clolumn_3 

1             4         7
2             5         8
3             6         9 

由于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

检查列名是否在列列表中。 如果没有做到10:

if("Col_4" in df.columns):
    print("Col_4 exists")
else:
    df["Col_4"] = 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM