简体   繁体   English

在每一列上执行操作

[英]Performing operation on each column

How can a loop be used to iterate through columns, applying the same function to each column in a pandas dataframe? 如何使用循环遍历列,将相同的函数应用于pandas数据框中的每一列?

For example, the following code normalizes the 'Cat1' Column. 例如,以下代码将“ Cat1”列标准化。

s['Cat1'] = (s.Cat1-s.Cat1.min())/(s.Cat1.max()-s.Cat1.min())

How can this code be extended so that the normalization is applied to Cat1...Cat50? 如何扩展此代码,以便将归一化应用于Cat1 ... Cat50?

Assuming s is a dictionary, the term s.Cat1 doesn't make any sense. 假设s是字典,则术语s.Cat1没有任何意义。

If s is a class, then now s['Cat1'] doesn't make any sense because class has no attribute __getitem__ 如果s是一个类,则现在s ['Cat1']没有任何意义,因为class没有属性__getitem__

So I am not sure what s is in your example... But if you want an easy way to generate the strings 'Cat1',...,'Cat50', you can use: 因此,我不确定您的示例中是什么...但是,如果您想要一种简单的方法来生成字符串“ Cat1”,...,“ Cat50”,则可以使用:

for i in range(1,51):
    name = 'Cat%s' % i
    # s[name] = ... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM