SheeHis
SheeHis

Reputation: 27

how do I create a collection off of a dataframe

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?

Upvotes: 0

Views: 47

Answers (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.

Upvotes: 1

Related Questions