gpt4 book ai didi

python - 将函数应用于 python-pandas 中的数据框时出现 ValueError

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:48 24 4
gpt4 key购买 nike

假设我有一个非常简单的数据框:

import pandas as pd
df = pd.DataFrame(np.full((6), 1))

现在我将定义一个函数,生成一个随机长度的 numpy 数组并将给定值添加到尾部:

import numpy as np
def func(row):
l = np.full((np.random.random_integer(5)), 1)
return np.hstack(l, row)

当我尝试将函数应用于df以获取二维数组时:

df.apply(func, axis=1),

我遇到这样的错误:

ValueError: Shape of passed values is (6, 2), indices imply (6, 1)

您知道问题是什么以及如何解决吗?先感谢您!

最佳答案

首先你想要np.random.random_integers,其次hstack需要一个元组,所以传递一个元组,第三你需要返回一些它可以与之对齐的东西,所以在这个案例系列:

In [213]:
df = pd.DataFrame(np.full((6), 1))
def func(row):
l = np.full((np.random.random_integers(5)), 1)
return pd.Series(np.hstack((l, row)))

In [214]:
df.apply(func, axis=1)

Out[214]:
0 1 2 3 4 5
0 1.0 1.0 1.0 NaN NaN NaN
1 1.0 1.0 NaN NaN NaN NaN
2 1.0 1.0 NaN NaN NaN NaN
3 1.0 1.0 1.0 NaN NaN NaN
4 1.0 1.0 1.0 1.0 1.0 NaN
5 1.0 1.0 1.0 1.0 1.0 1.0

请注意,我收到了大量有关上述内容的警告:

C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(3, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(2, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(1, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(4, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(5, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)

从 df 调用属性 values 获取 np 数组:

df.apply(func, axis=1).values

关于python - 将函数应用于 python-pandas 中的数据框时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38093398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com