Reputation: 2978
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
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