[英]Modify pandas dataframe values with numpy array
I'm trying to modify the values field of a pandas data frame with a numpy array [same size]. 我正在尝试使用numpy数组[相同大小]修改pandas数据帧的values字段。 something like this does not work
这样的事情不起作用
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? 任何替代品?
The .values
attribute 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
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, ie 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
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.