3

I have a dataframe with many metric columns all containing float output. I need to round them all to four digits. I want to loop through all the columns to do this.

import numpy as np
import pandas as pd

test_df = pd.DataFrame(np.random.randn(10,4), columns=['a','b','c','d'])

metrics = test_df.columns
metrics = metrics.tolist()

for x in metrics:
    test_df.x = np.round(test_df.x, 4)

However, this gives me the error:

AttributeError: 'DataFrame' object has no attribute 'x'

Whats the best way to do this?

0

1 Answer 1

4
import functools
test_df.apply(functools.partial(np.round, decimals=4))

if you want to iterate through columns, it's straightforward:

for c in test_df.columns:
    test_df[c] = np.round(test_df[c], 4)

what you tried to do that's busted has to do with attribute access in python. when you try to do test_df.x, that x has absolutely nothing to do with the x in your for loop. this would have the same result:

for unused_value in metrics:
    test_df.x = ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.