简体   繁体   English

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

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

相关问题 如何检查 DataFrame 列中是否存在元组值 - How to check if a tuple value exists in a DataFrame column 检查一个数据框中的值是否存在于另一个数据框中并创建列 - Check if value from one dataframe exists in another dataframe and create column 如何根据值所在的列名创建新的数据框? - How to create a new dataframe according to it column name where the value is existed? 如何创建密钥字典:column_name和value:python中来自数据框的列中的唯一值 - How to create a dictionary of key : column_name and value : unique values in column in python from a dataframe 根据值是否存在于另一个DataFrame列中在Pandas DataFrame中创建列 - Create column in a pandas DataFrame based on whether value exists in a different DataFrame column PySpark-在数据框中将默认列名作为“值” - PySpark- getting default column name as "value" in the dataframe 如何在 dataframe 的 A 列中找到 B 列中的 dataframe 值,如果是,将 B 列中的值替换为 A 列的值? - How do I find in dataframe value in column B exists in Column A in a dataframe, and if so, replace the value in column B with Column A's value? 使用两个数据框如何比较查找值作为 substring 在另一个 dataframe 的列中创建一个新列,如果匹配存在 - Using two dataframes how can I compare a lookup value as a substring in the column in another dataframe to create a new column if the match exists 查看DataFrame中是否存在值 - See if a value exists in a DataFrame 如何使用重复的字符串值创建数据框列? - How to create a dataframe column with repeated string value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM