Pandas: add a column if it does not exist

I am new to using pandas and writing a script where I read in a data framework and then do some calculations on some columns.

Sometimes I will have a β€œMet” column:

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

In other cases, I will have:

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

I need to do some calculations with the β€œMet” column, so if it is missing, I will need to calculate it using:

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

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

+6
source share
1 answer

You check it as follows:

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

Source: https://habr.com/ru/post/975438/


All Articles