Python,熊猫数据帧:为什么可以';将数据直接分配给df.values=df.values/100?

Python,熊猫数据帧:为什么可以';将数据直接分配给df.values=df.values/100?,python,pandas,Python,Pandas,我想 1) 从FamaFrench网站读取数据 2) 将日期(月、年)转换为DateTime对象 3) 将所有退货数据转换为百分比退货(退货/100) 我下面的代码读取来自famafrench网站的数据 industry30Web =web.DataReader("30_Industry_Portfolios","famafrench") industry30_monthlyVal = industry30Web[4] dateInt = industry30_monthlyVal.index

我想 1) 从FamaFrench网站读取数据 2) 将日期(月、年)转换为DateTime对象 3) 将所有退货数据转换为百分比退货(退货/100)

我下面的代码读取来自famafrench网站的数据

industry30Web =web.DataReader("30_Industry_Portfolios","famafrench")
industry30_monthlyVal = industry30Web[4]
dateInt = industry30_monthlyVal.index
conv = lambda x: datetime.datetime.strptime(str(x),'%Y%m')
dates = [conv(x) for x in dateInt]
industry30_monthlyVal.index =  dates
industry30_monthlyVal.values = industry30_monthlyVal.values/100 
最后一行显示AttributeError

请帮助并让我知道我做错了什么。

特别声明您不能将值分配给
属性


但是,当我查看pd.DataFrame的generic下的源代码时,您可以通过执行以下操作来实现您想要的:

@property
def values(self):
    """Numpy representation of NDFrame

    Notes
    -----
    The dtype will be a lower-common-denominator dtype (implicit
    upcasting); that is to say if the dtypes (even of numeric types)
    are mixed, the one that accommodates all will be chosen. Use this
    with care if you are not dealing with the blocks.

    e.g. If the dtypes are float16 and float32, dtype will be upcast to
    float32.  If dtypes are int32 and uint8, dtype will be upcase to
    int32.
    """
    return self.as_matrix()
该方法无法将数据写入数据帧(例如,可以覆盖的类变量)。 功能可能正是您想要的:

import numpy as np, pandas as pd
s = pd.dataFrame(np.random.randn(10))
s = s.apply(lambda x: x/100)

它对数据帧的工作方式相同

您只需执行
industry30\u monthlyVal=industry30\u monthlyVal/100