Home > Enterprise >  I get an error of ValueError: Unable to coerce to Series, length must be 1: given 0
I get an error of ValueError: Unable to coerce to Series, length must be 1: given 0

Time:10-12

I want to run this code in python but I receive this error : ValueError: Unable to coerce to Series, length must be 1: given 0. I have read a CSV file and want to run this code:

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)  

Error:

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

CodePudding user response:

OK, there's so much you haven't told us, but here's the problem.

Your handling of the DataFrame is wrong. You are passing the dataframe to your function with the parameter list (*args). That turns the dataframe into an iterator, and when you iterate a dataframe, it comes out as a set of columns. So, args ends up as a list containing the single column of data, as a pandas Series. When you do for i in args:, i ends up as that whole column. You end up doing your math on the whole column, but when you try to add the column to the list, that operation is not supported.

I'm ASSUMING your CSV file has one column of numbers, and you want to the math in your loop on every row of that column. If so, this is what you want:

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']))

With this input:

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

I get this output:

     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]
  • Related