首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我得到了一个ValueError错误:无法强迫串联,长度必须为1:给定0

我得到了一个ValueError错误:无法强迫串联,长度必须为1:给定0
EN

Stack Overflow用户
提问于 2022-10-10 22:38:05
回答 1查看 82关注 0票数 -1

我想在python中运行这段代码,但是我收到了以下错误:ValueError: Unable to coerce to Series, length must be 1: given 0。我已经读取了一个CSV文件,并希望运行以下代码:

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from numpy.core.fromnumeric import sort

## import data set

data = pd.read_csv('trial_incomes.csv')

print(data.head(10))

def H_function(*args):
    Hash=[]
    for i in args:
        hashedfunction=(6*(i)+1) % 5
        Hash+=hashedfunction
        print (Hash)
        
H_function(data)  

错误:

代码语言:javascript
运行
复制
ValueError                                Traceback (most recent call last)
Input In [58], in <cell line: 9>()
      5         Hash+=hashedfunction
      6         print (Hash)
----> 9 H_function(data)

Input In [58], in H_function(*args)
      3 for i in args:
      4     hashedfunction=(6*(i)+1) % 5
----> 5     Hash+=hashedfunction
      6     print (Hash)

File ~\miniforge3\lib\site-packages\pandas\core\ops\common.py:72, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     68             return NotImplemented
     70 other = item_from_zerodim(other)
---> 72 return method(self, other)

File ~\miniforge3\lib\site-packages\pandas\core\arraylike.py:107, in OpsMixin.__radd__(self, other)
    105 @unpack_zerodim_and_defer("__radd__")
    106 def __radd__(self, other):
--> 107     return self._arith_method(other, roperator.radd)

File ~\miniforge3\lib\site-packages\pandas\core\frame.py:7584, in DataFrame._arith_method(self, other, op)
   7581 axis = 1  # only relevant for Series other case
   7582 other = ops.maybe_prepare_scalar_for_op(other, (self.shape[axis],))
-> 7584 self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)
   7586 new_data = self._dispatch_frame_op(other, op, axis=axis)
   7587 return self._construct_result(new_data)

File ~\miniforge3\lib\site-packages\pandas\core\ops\__init__.py:283, in align_method_FRAME(left, right, axis, flex, level)
    279         raise ValueError(
    280             f"Unable to coerce list of {type(right[0])} to Series/DataFrame"
    281         )
    282     # GH17901
--> 283     right = to_series(right)
    285 if flex is not None and isinstance(right, ABCDataFrame):
    286     if not left._indexed_same(right):

File ~\miniforge3\lib\site-packages\pandas\core\ops\__init__.py:240, in align_method_FRAME.<locals>.to_series(right)
    238 else:
    239     if len(left.columns) != len(right):
--> 240         raise ValueError(
    241             msg.format(req_len=len(left.columns), given_len=len(right))
    242         )
    243     right = left._constructor_sliced(right, index=left.columns)
    244 return right

ValueError: Unable to coerce to Series, length must be 1: given 0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-11 03:12:39

好吧,有那么多你还没告诉我们,但问题是。

您对DataFrame的处理是错误的。您使用参数列表(*args)将数据文件传递给您的函数。这会将dataframe转换为迭代器,当您迭代一个dataframe时,它会以一组列的形式出现。因此,args最终成为了一个包含单列数据的列表,作为一个熊猫系列。当您执行for i in args:时,i作为整个专栏收尾。最后对整个列进行数学运算,但是当您试图将该列添加到列表中时,不支持该操作。

我假设您的CSV文件有一列数字,并且您希望对该列的每一行进行循环中的计算。如果是这样的话,这就是你想要的:

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np

data = pd.read_csv('x.csv')

print(data.head(10))

def H_function(*args):
    Hash=[]
    for i in args:
        hashedfunction=(6*(i)+1) % 5
        Hash.append(hashedfunction)
    return Hash
        
print(H_function(data['income']))

有了这一投入:

代码语言:javascript
运行
复制
income
1234.00
2345.00
3456.00
4576.00
12000.00
24000.00
36000.00
48000.00
60000.00
921.11
922.22
933.33

我得到了这个输出:

代码语言:javascript
运行
复制
     income
0   1234.00
1   2345.00
2   3456.00
3   4576.00
4  12000.00
5  24000.00
6  36000.00
7  48000.00
8  60000.00
9    921.11
[0     0.00
1     1.00
2     2.00
3     2.00
4     1.00
5     1.00
6     1.00
7     1.00
8     1.00
9     2.66
10    4.32
11    0.98
Name: income, dtype: float64]
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74021314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档