[英]Python, Pandas Dataframes: Why can't I assign data directly to the df.values=df.values/100?
I want to 1) Read in data from the FamaFrench website 2) Convert the date (Month,Year) into a DateTime object 3) Convert all the returns data into percentage returns (returns/100) 我想要1)从FamaFrench网站上读取数据2)将日期(月,年)转换为DateTime对象3)将所有返回数据转换为百分比收益(returns / 100)
My code below reads in data from the famafrench website. 我下面的代码从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
The last line is showing an AttributeError 最后一行显示AttributeError
Please help and let me know what I'm doing wrong. 请帮助,让我知道我在做什么错。
The documentation specifically states that you cannot assign to the values
attribute. 该文档特别指出您不能分配给
values
属性。
However, you can achieve what you want by doing industry30_monthlyVal[:]= industry30_monthlyVal[:]/100
但是,您可以通过执行
industry30_monthlyVal[:]= industry30_monthlyVal[:]/100
When i had a look at the source under generic of pd.DataFrame i found: 当我在pd.DataFrame的泛型下查看源代码时,发现:
@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()
The method has no capability for writing data to the DataFrame (like ie an class variable that you can overwrite). 该方法不具备将数据写入DataFrame的功能(例如,可以覆盖的类变量)。 The apply functionality is probably what your are looking for:
应用功能可能是您正在寻找的功能:
import numpy as np, pandas as pd
s = pd.dataFrame(np.random.randn(10))
s = s.apply(lambda x: x/100)
It works the same way for a data frame 对于数据帧,其工作方式相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.