标准化 Pandas DataFrame 的每一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26721353/
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:37:45  来源:igfitidea点击:

Normalize each column of a pandas DataFrame

pythonpython-2.7pandasdataframe

提问by Nyxynyx

Each column of the Dataframe needs their values to be normalized according the value of the first element in that column.

Dataframe 的每一列都需要根据该列中第一个元素的值对其值进行规范化。

for timestamp, prices in data.iteritems():
    normalizedPrices = prices / prices[0]
    print normalizedPrices     # how do we update the DataFrame with this Series?

However how do we update the DataFrame once we have created the normalized column of data? I believe if we do prices = normalizedPriceswe are merely acting on a copy/view of the DataFrame rather than the original DataFrame itself.

但是,一旦我们创建了规范化的数据列,我们如何更新 DataFrame?我相信如果我们这样做,prices = normalizedPrices我们只是对 DataFrame 的副本/视图而不是原始 DataFrame 本身采取行动。

回答by Alex Riley

It might be simplest to normalize the entire DataFrame in one go (and avoid looping over rows/columns altogether):

一次标准化整个 DataFrame 可能是最简单的(并避免完全遍历行/列):

>>> df = pd.DataFrame({'a': [2, 4, 5], 'b': [3, 9, 4]}, dtype=np.float) # a DataFrame
>>> df
   a  b
0  2  3
1  4  9
2  5  4

>>> df = df.div(df.loc[0]) # normalise DataFrame and bind back to df
>>> df
     a         b
0  1.0  1.000000
1  2.0  3.000000
2  2.5  1.333333

回答by unutbu

Assign to data[col]:

分配给data[col]

for col in data:
    data[col] /= data[col].iloc[0]

回答by newbie

import numpy

data[0:] = data[0:].values/data[0:1].values