burhancigdem
burhancigdem

Reputation: 105

how to generate new dataframe?

I have a dataframe below:

df = pd.DataFrame({"p1" : [2,3,5], "p2" : [2,3,5], "s1": [10,20,30], "s2": [5,-2, 7]})
df

enter image description here

I want to get a new data frame, let's say df_new. df_new will have the same column labels as the df. the values of p1 and p2 for both dataframes will be the same, but for df_new the column values s1 and s2 will be produced with the formula below from df:

s1 = (s1 * p1 / p2) and s2 = (s2 * p1 / p2)

Upvotes: 1

Views: 44

Answers (1)

jezrael
jezrael

Reputation: 863166

Use DataFrame.mul with Series.div:

df[['s1','s2']] = df[['s1','s2']].mul(df.p1.div(df.p2), axis=0)

print (df)
   p1  p2    s1   s2
0   2   2  10.0  5.0
1   3   3  20.0 -2.0
2   5   5  30.0  7.0

EDIT: One idea is use DataFrame.filter with s substring

cols = df.filter(like='s').columns
#or if starts columns names by s
#cols = df.filter(regex='^s').columns
df[cols] = df[cols].mul(df.p1.div(df.p2), axis=0)

Or select last N columns, e.g. here 2 by DataFrame.iloc:

df.iloc[:, -2:] = df.iloc[:, -2:].mul(df.p1 / df.p2, axis=0)

Use DataFrame.assign:

df = df.assign(s1 = (df.s1 * df.p1 / df.p2),
               s2 = (df.s2 * df.p1 / df.p2))
print (df)

   p1  p2    s1   s2
0   2   2  10.0  5.0
1   3   3  20.0 -2.0
2   5   5  30.0  7.0

Or create new columns like:

df.s1 = (df.s1 * df.p1 / df.p2) 
df.s2 = (df.s2 * df.p1 / df.p2) 
print (df)
   p1  p2    s1   s2
0   2   2  10.0  5.0
1   3   3  20.0 -2.0
2   5   5  30.0  7.0

Upvotes: 2

Related Questions