在没有它的索引的情况下从另一个DataFrame复制数据

时间:2018-03-06 16:21:08

标签: python pandas

我有两个pandas DataFrame

其中一个是空的,并按照以下方式创建:

main_data = pd.DataFrame(columns='m10 m9 m8 m7 m6 m5 m4 m3 m2 m1 Day Price'.split())

另一个是带有相同列名的数据。我想复制这样的列:

main_data['m10'] = md['Price'][0:-1]
main_data['m9'] = md['Price'][1:-2]
main_data['m8'] = md['Price'][2:-3]
main_data['m7'] = md['Price'][3:-4]
main_data['m6'] = md['Price'][4:-5]
main_data['m5'] = md['Price'][5:-6]
main_data['m4'] = md['Price'][6:-7]
main_data['m3'] = md['Price'][7:-8]
main_data['m2'] = md['Price'][8:-9]
main_data['m1'] = md['Price'][9:-10]
main_data['Price'] = md['Price'][10:]
main_data['Day'] = md['Day'][10:]

m#只是第二天的价格,而价格和日是最后一天。我想要做的是将最后十个价格放在同一行和最后的价格和日期。

我在DataFrame中有4000行。我不确定我是否正在做正确的方法。这是我想要在行中完成的想法:

PriceDay1 PriceDay2 PriceDay3 ... PriceDay10 and FinalDayPrice and FinalDayDate 

这就是问题所在。当我将数据从md复制到main_data时,md的索引也复制到main。例如,main_data[m9]的第一行变为NaN,因为md [' m9']从索引1开始。如何解决此问题?还有什么更好的方法吗?

由于

编辑: 添加样本数据

"Date","Price","Open","Hi","Lo","Volume","Diff"
"14.03.2001","10.399,18","10.824,05","10.982,04","10.335,48","0","-3,93"
"15.03.2001","10.887,03","10.399,18","10.887,03","10.119,57","0","4,69"
"16.03.2001","11.191,77","10.887,03","11.361,73","10.769,92","0","2,80"
"19.03.2001","10.856,19","11.191,77","11.551,98","10.790,02","0","-3,00"
"20.03.2001","11.284,13","10.856,19","11.368,96","10.856,19","0","3,94"
"21.03.2001","10.952,04","11.284,13","11.438,85","10.894,71","0","-2,94"
"22.03.2001","10.662,25","10.952,04","10.980,85","10.465,05","0","-2,65"
"23.03.2001","10.619,43","10.662,25","10.797,66","10.519,57","0","-0,40"
"26.03.2001","10.598,85","10.619,43","10.748,71","10.493,71","0","-0,19"
"27.03.2001","10.129,49","10.598,85","10.598,85","10.068,42","0","-4,43"
"28.03.2001","9.699,53","10.129,49","10.129,49","9.219,91","0","-4,24"
"29.03.2001","9.073,53","9.699,53","9.699,53","8.962,83","0","-6,45"
"30.03.2001","10.177,57","9.073,53","10.201,48","9.073,53","0","12,17"

输出:

            m10         m9         m8         m7         m6         m5  \
0      10399.18        NaN        NaN        NaN        NaN        NaN   
1      10887.03   10887.03        NaN        NaN        NaN        NaN   
2      11191.77   11191.77   11191.77        NaN        NaN        NaN   
3      10856.19   10856.19   10856.19   10856.19        NaN        NaN   
4      11284.13   11284.13   11284.13   11284.13   11284.13        NaN   
5      10952.04   10952.04   10952.04   10952.04   10952.04   10952.04   
6      10662.25   10662.25   10662.25   10662.25   10662.25   10662.25   
7      10619.43   10619.43   10619.43   10619.43   10619.43   10619.43

1 个答案:

答案 0 :(得分:0)

向main_data添加新数据时,最简单的方法是创建一个额外的DataFrame对象并使用append函数。

如页面所述,您可以设置ignore_index=True

页面上的示例如下所示

>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
>>> df
   A  B
0  1  2
1  3  4
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
>>> df.append(df2)
   A  B
0  1  2
1  3  4
0  5  6
1  7  8