简体   繁体   English

如何从数据框创建集合

[英]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' float() 参数必须是字符串或数字,而不是 'dict'

How can i fix my code?我该如何修复我的代码?

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 .正如这个文档所说, interp()的第二个参数应该是一维浮点数序列,第三个应该是一维浮点数序列或 complex

But, your x2 and x1 is not one-directional float or complex sequence but just a dictionary.但是,您的x2x1不是单向浮点数或复杂序列,而只是一个字典。

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.我会解决你的抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM