user13954172
user13954172

Reputation: 13

Adding values to new columns in data frame

I have a large data frame (data_df) and have created four new columns. Say the original columns were 'A', 'B', 'C', 'D' and the four new ones are 'E', 'F', 'G', 'H'. For every row (3000), I need to add values to the new columns. In 'E' it needs to be A/A+B. For 'F' B/A+B. For 'G' C/C+D. And for 'H' D/C+D. I need to run this for every row.

Upvotes: 0

Views: 70

Answers (2)

safiqul islam
safiqul islam

Reputation: 650

Ans to you specific question

import pandas
df = pandas.DataFrame({'A':[1,2], 'B':[3,4],'C':[5,6], 'D':[7,8]})


df['E'] = df.apply(lambda row: row.A/(row.A + row.B), axis=1)
df['F'] = df.apply(lambda row: row.B/(row.A + row.B), axis=1)
df['G'] = df.apply(lambda row: row.C/(row.C + row.D), axis=1)
df['H'] = df.apply(lambda row: row.D/(row.C + row.D), axis=1)
print(df)

output

A  B  C  D         E         F         G         H
1  3  5  7  0.250000  0.750000  0.416667  0.583333
2  4  6  8  0.333333  0.666667  0.428571  0.571429

Upvotes: 1

a11
a11

Reputation: 3396

It is pretty simple/intuitive. A similar question is here, and below is an answer to your specific question.

import pandas as pd

### Make up data for example
A = [5, 8, 2, -1]
B = [1, 0, 1, 3]
df = pd.DataFrame(list(zip(A,B)), columns =['A', 'B'])
display(df)

enter image description here

### Calculate Column E
df['E'] = df['A'] / (df['A'] + df['B'])
display(df)

enter image description here

Upvotes: 1

Related Questions