Home > Enterprise >  Create and drop columns in Pandas
Create and drop columns in Pandas

Time:11-25

I have a big data frame with minute wise data as below, where i do some functions and then drop columns.

datetime E10 E11 E12 E132 E21 E31 E32 E37 E40 E43 E44 E45 E47 E65 E8 E28 E41 E46 E48 E50 E51 E52 E53 E54
24-11-2021 07:30 22 122 62 232 55 874.2 32.8 351.2 1.4 0.3 5.1 4 1 24.2 76 0 0 0 0 0 0 0 0 0
24-11-2021 07:31 23 120 60 232 55 0 33.3 0 1.3 0.3 0 2 1 24.7 80 0 0 0 0 0 0 0 0 0
24-11-2021 07:32 22 123 61 208 54 0 32.9 0 1 0.3 0 3 1 24.7 79 0 0 0 0 0 0 0 0 0
24-11-2021 07:33 21 120 54 296 55 0 33 0 0.9 0.3 0 2 1 24.9 79 0 0 0 0 0 0 0 0 0
24-11-2021 07:34 21 122 58 272 50 0 32.4 0 0.9 0.3 0 3 1 24.3 77 0 0 0 0 0 0 0 0 0
# Arithmetic Functions
df['E08'] = df['E8']   df['E132'].multiply(0.1467)
df['E037'] = df['E37'] - df['E65']
df['E032'] = df['E32'] - df['E40']
df['E010'] = df['E10']   df['E45']   (df['E28'] - df['E48'])
df['E011'] = df['E11'] - df['E44'] - df['E43']
df['E021'] = df['E21'] - df['E50'] - df['E51'] - df['E52'] - df['E53'] - df['E54']
df['E031'] = df['E31'] - df['E41']


# Drop
df.drop([
    'E8', 'E132', 'E37', 'E65', 'E32', 'E40', 'E10', 'E45',
    'E48', 'E11', 'E44', 'E43', 'E21', 'E50', 'E51', 'E52',
    'E53', 'E54', 'E31', 'E41'], axis=1, inplace=True)

I would like to know which is the efficient method

Question 1: Creating a new column or keeping the same column. Example: Option 1: df['E8'] = df['E8'] df['E132'].multiply(0.1467) or Option 2: df['E08'] = df['E8'] df['E132'].multiply(0.1467)

Question 2: Should i apply drop code separately like above, or is there a way to drop 'E8' & 'E132' once E08 is created. I tried df['E08'] = df['E8'] df['E132'].multiply(0.1467).drop(['E8', 'E132'], axis=1, inplace=True)- its not working

CodePudding user response:

If want use and then remove column from original use DataFrame.pop, then drop is not necessary:

df['E08'] = df.pop('E8')   df.pop('E132').multiply(0.1467)
df['E037'] = df.pop('E37') - df.pop('E65')
...
  • Related