0

I have this code

y2 = {}
x1=datasim.iloc[:,1]
y1=datasim.iloc[:, ::2]
x2=[i*(0.1) for i in range(4800)]
x2 = {}
for column in x1:
    xb=[i*(0.1) for i in range(4800)]
    x2[column]=pd.DataFrame(xb)
for column in y1:
    y2[y1.columns.get_loc(column)]=np.interp(x2,x1,y1[column])

When I execute the code I get following error message:

float() argument must be a string or a number, not 'dict'

How can i fix my code?

mischva11
  • 2,811
  • 3
  • 18
  • 34
SheeHis
  • 21
  • 1
  • 6
  • 1
    in which line are you getting this error? – M.K Mar 04 '20 at 14:47
  • I am getting the error on the last line – SheeHis Mar 04 '20 at 15:00
  • 1
    We cannot reproduce your code. Post a code that we can copy/paste and reproduce it. What is `datasim`? – M.K Mar 04 '20 at 15:40
  • 1
    Please share the entire error message as well as a [mcve]. – AMC Mar 04 '20 at 20:41
  • datasim is a dataframe and the full message error is as follows: Traceback (most recent call last): File "", line 11, in y2[y1.columns.get_loc(column)]=np.interp(x2,x1,y1[column]) File "<__array_function__ internals>", line 6, in interp File "C:\Users\shengesh\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1412, in interp return interp_func(x, xp, fp, left, right) TypeError: float() argument must be a string or a number, not 'dict' – SheeHis Mar 05 '20 at 07:28

1 Answers1

1

As this doc tells, the second argument of interp() should be 1-D sequence of floats and the third one should be 1-D sequence of float or complex.

But, your x2 and x1 is not one-directional float or complex sequence but just a dictionary.

Now, it depends on how exactly you want your code to be changed.


Make the dictionary as a list

from collections import defaultdict

def flattenit(sublist):
    flat_list = []
    for sublist in l:
        for item in sublist:
            flat_list.append(item)

# so on ...

y2 = {}
x1=datasim.iloc[:,1]

sub_x1 = defaultdict(list)
for k, v in x1:
    sub_x1[k].append(v)

y1=datasim.iloc[:, ::2]
x2=[i*(0.1) for i in range(4800)]
x2 = {}

sub_x2 = defaultdict(list)
for k, v in x2:
    sub_x2[k].append(v)

for column in x1:
    xb=[i*(0.1) for i in range(4800)]
    x2[column]=pd.DataFrame(xb)
for column in y1:
    y2[y1.columns.get_loc(column)]=np.interp(flattenit( sub_x2.keys() ),
                                             flattenit( sub_x1.keys() ),
                                             y1[column])

# so on 'til an end...


# Oh, by the way, this couple of a stackoverflow posts helped to write this code:
# https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists
# https://stackoverflow.com/questions/960733/python-creating-a-dictionary-of-lists 

For each bunch or single of keys, let 'em ongoing on each different 1-D list in one 2-D list

# so on ...

y2 = [] #sorry for your y2! but now it's a list.
x1=datasim.iloc[:,1]
y1=datasim.iloc[:, ::2]
x2=[i*(0.1) for i in range(4800)]
x2 = {}
for column in x1:
    xb=[i*(0.1) for i in range(4800)]
    x2[column]=pd.DataFrame(xb)
for column in y1:
    for i in len(x2.keys()):
        y2.append([])
        y2[i].append([])
        for j in len(x1.keys()):
            y2[i].append(np.interp(x2[i], x1[j], y1[column]))

# so on 'til an end

If one or all of the codes are wrong or something bad happened with it, please tell me. I'll fix what you are complaining with.