Python 熊猫:如果不存在则添加列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25896453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:48:00  来源:igfitidea点击:

Pandas: Add column if does not exists

pythonpandas

提问by user2165857

I'm new to using pandas and am writing a script where I read in a dataframe and then do some computation on some of the columns.

我是使用 Pandas 的新手,正在编写一个脚本,在其中读取数据帧,然后对某些列进行一些计算。

Sometimes I will have the column called "Met":

有时我会有一个名为“Met”的专栏:

df = pd.read_csv(File, sep='\t', compression='gzip', header=0, names=["Chrom", "Site", "coverage", "Met"]) 

Othertimes I will have:

其他时候我会有:

df = pd.read_csv(File, sep='\t', compression='gzip', header=0, names=["Chrom", "Site", "coverage", "freqC"]) 

I need to do some computation with the "Met" column so if it isn't present I will need to calculate it using:

我需要对“Met”列进行一些计算,因此如果它不存在,我将需要使用以下方法计算它:

df['Met'] = df['freqC'] * df['coverage'] 

is there a way to check if the "Met" column is present in the dataframe, and if not add it?

有没有办法检查数据框中是否存在“Met”列,如果没有,则添加它?

采纳答案by YS-L

You check it like this:

你这样检查:

if 'Met' not in df:
    df['Met'] = df['freqC'] * df['coverage'] 

回答by autonopy

If you were creating the dataframe from scratch, you could create the missing columns without a loop merely by passing the column names into the pd.DataFrame()call:

如果您是从头开始创建数据框,则只需将列名传递到pd.DataFrame()调用中,就可以在没有循环的情况下创建缺失的列:

cols = ['column 1','column 2','column 3','column 4','column 5']
df = pd.DataFrame(list_or_dict, index=['a',], columns=cols)