pandas 数据帧ValueError:传递值的形状为(X,),索引暗示(X,Y) [英] Pandas Dataframe ValueError: Shape of passed values is (X, ), indices imply (X, Y)

查看:5937
本文介绍了 pandas 数据帧ValueError:传递值的形状为(X,),索引暗示(X,Y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误,我不知道如何解决它。

I am getting an error and I'm not sure how to fix it.

以下内容似乎有效:

def random(row):
   return [1,2,3,4]

df = pandas.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))

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

和我的输出是:

[1,2,3,4]
[1,2,3,4]
[1,2,3,4]
[1,2,3,4]

但是,当我将其中一列更改为1或None的值时:

However, when I change one of the of the columns to a value such as 1 or None:

def random(row):
   return [1,2,3,4]

df = pandas.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))
df['E'] = 1

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

我收到错误:

ValueError: Shape of passed values is (5,), indices imply (5, 5)

我一直在摔跤现在几天,似乎没有任何工作。有趣的是,当我更改

I've been wrestling with this for a few days now and nothing seems to work. What is interesting is that when I change

def random(row):
   return [1,2,3,4]

to

def random(row):
   print [1,2,3,4]


$ b一切似乎都正常工作。

everything seems to work normally.

这个问题是提出这个问题,我觉得可能会令人困惑。

This question is a clearer way of asking this question, which I feel may have been confusing.

我的目标是计算每行的列表,然后创建一个列。

My goal is to compute a list for each row and then create a column out of that.

编辑:我最初是从一个数据框开始,一个列。我添加4列4个差异申请步骤,然后当我尝试添加另一列我得到这个错误。

I originally start with a dataframe that hase one column. I add 4 columns in 4 difference apply steps, and then when I try to add another column I get this error.

推荐答案

如果您的目标是向DataFrame添加新列,只需将函数写入返回标量值(不是列表)的函数,像这样:

If your goal is add new column to DataFrame, just write your function as function returning scalar value (not list), something like this:

>>> def random(row):
...     return row.mean()

然后使用申请:

>>> df['new'] = df.apply(func = random, axis = 1)
>>> df
          A         B         C         D       new
0  0.201143 -2.345828 -2.186106 -0.784721 -1.278878
1 -0.198460  0.544879  0.554407 -0.161357  0.184867
2  0.269807  1.132344  0.120303 -0.116843  0.351403
3 -1.131396  1.278477  1.567599  0.483912  0.549648
4  0.288147  0.382764 -0.840972  0.838950  0.167222

我不知道如果您的新列可能包含列表,但它无限可能包含元组((...)而不是 [...] ):

I don't know if it possible for your new column to contain lists, but it deinitely possible to contain tuples ((...) instead of [...]):

>>> def random(row):
...    return (1,2,3,4,5)
...
>>> df['new'] = df.apply(func = random, axis = 1)
>>> df
          A         B         C         D              new
0  0.201143 -2.345828 -2.186106 -0.784721  (1, 2, 3, 4, 5)
1 -0.198460  0.544879  0.554407 -0.161357  (1, 2, 3, 4, 5)
2  0.269807  1.132344  0.120303 -0.116843  (1, 2, 3, 4, 5)
3 -1.131396  1.278477  1.567599  0.483912  (1, 2, 3, 4, 5)
4  0.288147  0.382764 -0.840972  0.838950  (1, 2, 3, 4, 5)

这篇关于 pandas 数据帧ValueError:传递值的形状为(X,),索引暗示(X,Y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆