question.it
question.it

Reputation: 2978

How to consider precision in another column while applying round(precision) to the value column in Dataframe, Python 3.6

I have DataFrame with Column: precision & value; precision contains integer values like 1, 2, 3-decimal places to pass in round function. How to pass "precision" field while performing round to each value? For every value I have to apply round as per field "precision".

df = pd.DataFrame([[15.5666, 2], [14.32, 1], [14.68954, 3], [16.78, 1]], columns=['value', 'precision'])

df['value2'] = df['value'].round(df['precision'])

Upvotes: 1

Views: 66

Answers (1)

BENY
BENY

Reputation: 323306

Sometime for loop is not bad

[round(x, y) for x , y in zip(df['value'], df['precision'])]
Out[128]: [15.57, 14.3, 14.69, 16.8]
#df['value2'] = [round(x, y) for x , y in zip(df['value'], df['precision'])]

Upvotes: 3

Related Questions