Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through columns in a dataframe?

Tags:

python

pandas

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?

like image 318
analyticsPierce Avatar asked May 06 '26 21:05

analyticsPierce


1 Answers

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 = ...
like image 70
acushner Avatar answered May 09 '26 09:05

acushner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!