简体   繁体   English

Python,Pandas数据框:为什么我不能直接将数据分配给df.values = df.values / 100?

[英]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.

相关问题 什么是 df.values[:,1:]? - What is df.values[:,1:]? 为什么pandas df.values 将元组转换成字符串 - Why does pandas df.values convert tuple into strings Pandas df.values不返回索引值 - Pandas df.values does not return index values Python Pandas 如何将 JSON 值分配给 Pandas DF - Python Pandas How to Assign JSON Values to a Pandas DF 熊猫,为什么我不能用 df.loc[condition, 'col'] = df.loc[condition, 'col'].modification 赋值? - Pandas, why can't I assign with df.loc[condition, 'col'] = df.loc[condition, 'col'].modification? 在pandas df中重新分配列值 - Re-assign column values in a pandas df 熊猫数据框:是否可以为列名和/或df值分配标签? - Pandas dataframe: Can you assign a label for the column names and/or the df values? 如何在 Python 中使用 Pandas DF 值作为字符串,以便我可以使用从 Pandas DF 中提取的确切值在 Selenium 中发送密钥? - How to use Pandas DF values as string in Python so i can sendkeys in Selenium with the exact valeu extracted from Pandas DF? Python Pandas 比较具有相似(字符串)列的两个数据帧,其中 1 个 df 的值是另一个 df 值的子字符串 - Python Pandas compare two dataframes with a similar (string) column, where 1 df's values are substrings of the other df's values Pandas df 中的度假村价值 - Resort values in a pandas df
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM