使用 numpy 数组修改 Pandas 数据帧值

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

Modify pandas dataframe values with numpy array

pythonnumpypandas

提问by Bobo

I'm trying to modify the values field of a pandas data frame with a numpy array [same size]. something like this does not work

我正在尝试使用 numpy 数组 [相同大小] 修改 Pandas 数据框的值字段。这样的事情不起作用

import pandas as pd
# create 2d numpy array, called arr
df = pd.DataFrame(arr, columns=some_list_of_names)
df.values = myfunction(arr)

any alternatives?

任何替代方案?

回答by Andy Hayden

The .valuesattribute is often a copy - especially for mixed dtypes (so assignment to it is not guaranteed to work - in newer versions of pandas this will raise).

.values属性通常是一个副本 - 特别是对于混合 dtypes(因此不能保证对其进行赋值 - 在较新版本的Pandas中这会引发)。

You should assign to the specific columns (note the order is important).

您应该分配给特定的列(注意顺序很重要)。

df = pd.DataFrame(arr, columns=some_list_of_names)
df[some_list_of_names] = myfunction(arr)


Example (in pandas 0.15.2):

示例(在Pandas 0.15.2 中):

In [11]: df = pd.DataFrame([[1, 2.], [3, 4.]], columns=['a', 'b'])

In [12]: df.values = [[5, 6], [7, 8]]
AttributeError: can't set attribute

In [13]: df[['a', 'b']] = [[5, 6], [7, 8]]

In [14]: df
Out[14]:
   a  b
0  5  6
1  7  8

In [15]: df[['b', 'a']] = [[5, 6], [7, 8]]

In [16]: df
Out[16]:
   a  b
0  6  5
1  8  7

回答by Dr. Jan-Philip Gehrcke

I think this is the method you are looking for:

我认为这是您正在寻找的方法:

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.applymap.html

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.applymap.html

Apply a function to a DataFrame that is intended to operate elementwise, i.e. like doing map(func, series) for each series in the DataFrame

将函数应用于旨在按元素进行操作的 DataFrame,例如为 DataFrame 中的每个系列执行 map(func, series)

Example:

例子:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame(np.random.rand(3,4), columns = list('abcd'))
>>> df
          a         b         c         d
0  0.394819  0.662614  0.752139  0.396745
1  0.802134  0.934494  0.652150  0.698127
2  0.518531  0.582429  0.189880  0.168490
>>> f = lambda x: x*100
>>> df.applymap(f)
           a          b          c          d
0  39.481905  66.261374  75.213857  39.674529
1  80.213437  93.449447  65.215018  69.812667
2  51.853097  58.242895  18.988020  16.849014
>>>