规范化熊猫数据帧的每一列



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

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

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

一次性规范化整个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

分配给data[col]:

for col in data:
    data[col] /= data[col].iloc[0]
import numpy
data[0:] = data[0:].values/data[0:1].values 

最新更新