Bitopan Gogoi
Bitopan Gogoi

Reputation: 125

Rounding off a dataframe column based on other column

I have a dataframe with two numeric columns col"A" and col"B". I want to create a col"C" by rounding off col"A" taking significant_figure/decimal input from col"B". What is the way to achieve it?

Upvotes: 1

Views: 54

Answers (1)

jezrael
jezrael

Reputation: 862481

Use custom lambda function or lsit comprehension with zip:

df = pd.DataFrame({'A': [0.1256, 0.25369],
                    'B':[2, 3]})

df['C'] = df.apply(lambda x: round(x.A, int(x.B)), axis=1)
#alternative solution
#df['C'] = [round(x, y) for x, y in zip(df.A, df.B)]

print (df)
         A  B      C
0  0.12560  2  0.130
1  0.25369  3  0.254

Upvotes: 1

Related Questions