analyticsPierce
analyticsPierce

Reputation: 3025

How to loop through columns in a dataframe?

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?

Upvotes: 3

Views: 14140

Answers (1)

acushner
acushner

Reputation: 9946

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 = ...

Upvotes: 4

Related Questions