pandas 如何遍历数据框中的列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24417485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:12:03  来源:igfitidea点击:

How to loop through columns in a dataframe?

pythonpandas

提问by analyticsPierce

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?

什么是最好的方法来做到这一点?

回答by acushner

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 xhas absolutely nothing to do with the xin your forloop. this would have the same result:

你试图做的事情被破坏了与python中的属性访问有关。当您尝试这样做test_df.x,那x绝对没有任何跟x在你的for循环。这将产生相同的结果:

for unused_value in metrics:
    test_df.x = ...