如何从多个循环附加到数据框 [英] How to append to a data frame from multiple loops

查看:55
本文介绍了如何从多个循环附加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,该代码从csv接收文件并收取价格差,但为简单起见,我制作了一个可重现的示例,如下所示.我想将每个结果附加到特定列名称的末尾.例如,第一个循环将通过大小1和分钟1,因此对于文件2,文件3,文件4,它应追加到列名1; 1后面.所以输出应该是:

I have a code, which takes in files from csv and takes a price difference, but to make it simplar I made a reproducible example as seen below. I want to append each result to the end of a specific column name. For example the first loop will go through size 1 and minute 1 so it should append to column names 1;1, for file2, file 3, file4. So the output should be :

1;1  1;2   1;3   2;1  2;2  2;3      
0    0     0       same below as for 1
0    0     0
2    2     2
2    2     2
4    4     4
4    4     4
5    5     5
0    0     0
0    0     0
0    0     0
2    2     2
2    2     2
4    4     4
4    4     4
6    6     6
6    6     6
0    0     0
0    0     0
0    0     0
2    2     2
2    2     2
4    4     4
4    4     4
6    6     6
7    7     7

我正在使用循环来设置带前缀的数据框列,因为在我的原始代码中,分钟,大小和文件的数量是由用户输入的.

I am using a loop to set prefixed data frame columns, because in my original code the number of minutes, sizes, and files is inputted by the user.

import numpy as np
import pandas as pd
file =[1,2,3,4,5,6,6,2]
file2=[1,2,3,4,5,6,7,8]
file3=[1,2,3,4,5,6,7,9]
file4=[1,2,1,2,1,2,1,2]
size=[1,2]
minutes=[1,2,3]
list1=[file,file2,file3]
data=pd.DataFrame(file)
data2=pd.DataFrame(file2)
data3=pd.DataFrame(file3)
list1=(data,data2,data3)
datas=pd.DataFrame(file4)
col_names = [str(sizer)+';'+str(number) for sizer in size for number in minutes]
datanew=pd.DataFrame(columns=col_names)


for sizes in size:
    for minute in minutes:
        for files in list1:
            pricediff=files-data
             datanew[str(sizes)+';'+str(minute)]=datanew[str(sizes)+';'+str(minute)].append(pricediff,ignore_index=True)
print(datanew)

尝试以下行时:datanew=datanew.append({str(sizes)+';'+str(minute): df['pricediff']},ignore_index=True)它附加了数据,但结果不是干净的"

When trying this line: datanew=datanew.append({str(sizes)+';'+str(minute): df['pricediff']},ignore_index=True) It appends the data but the result isn't "clean"

我原始数据的结果告诉我:

The result from my original data, gives me this:

    111;5.0,1111;5.0
"0                                          4.5
1                                          0.5
2                                            8
3                                            8
4                                            8
                        ...                   
704                                        3.5
705                                        0.5
706                                       11.5
707                                        0.5
708                                        9.0
Name: pricediff, Length: 709, dtype: object",
"price    0.0
0        0.0
Name: pricediff, dtype: float64",
"0      6.5
1      6.5
2      3.5
3     13.0
Name: pricediff, Length: 4, dtype: float64",

推荐答案

IIUC您正在寻找的是:

IIUC what you are looking for is:

datanew=datanew.append({str(sizes)+';'+str(minute): pricediff}, ignore_index=True)

之所以会发生这种情况,是因为您不能在不修改整个数据帧长度的情况下更改数据帧单列的长度.

This happens because you cannot change length of a single column of a dataframe without modifying length of the whole data frame.

现在以下面的示例为例:

Now consider the below as an example:

import pandas as pd

df=pd.DataFrame({"a": list("xyzpqr"), "b": [1,3,5,4,2,7], "c": list("pqrtuv")})

print(df)

#this will fail:
#df["c"]=df["c"].append("abc", ignore_index=True)
#print(df)

#what you can do instead:
df=df.append({"c": "abc"}, ignore_index=True)

print(df)

#you can even create new column that way:
df=df.append({"x": "abc"}, ignore_index=True)

修改

为了附加pd.Series,请执行相同的操作:

In order to append pd.Series do literally the same:

abc=pd.Series([-1,-2,-3], name="c")
df=df.append({"c": abc}, ignore_index=True)

print(df)

abc=pd.Series([-1,-2,-3], name="x")
df=df.append({"x": abc}, ignore_index=True)

这篇关于如何从多个循环附加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆