Pandas:如何迭代两个格式完全相同的数据帧? [英] Pandas: How could I iterate two dataframes which have exactly same format?

查看:61
本文介绍了Pandas:如何迭代两个格式完全相同的数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是制作包含一对对应数据框位置的列表,如下所示

My final goal is making list which contain a pair for corresponding location of dataframes, like below

 [df_one_first_element, df_two_first_element, column_first, index_first]

 :[0.619159, 0.510162, 20140109,0.50], [0.264191,0.269053,20140213,0.50]...

所以我试图迭代两个数据帧,但现在卡住了.如何迭代两个格式完全相同但数据不同的数据帧.

So I am trying to iterate two dataframe but got stuck now. How could I iterate two dataframe which has exactly same format but different data.

例如,我有两个数据框;df_one 和 df_two 如下所示:

For example, I have two dataframes; df_one and df_two that appear like the below:

df_one = 

      20140109  20140213  20140313  20140410  20140508  20140612  20140710  \
0.50  0.619159  0.264191  0.438849  0.465287  0.445819  0.412582  0.397366   
0.55  0.601379  0.303953  0.457524  0.432335  0.415333  0.382093  0.382361  

df_two = 

      20140109  20140213  20140313  20140410  20140508  20140612  20140710  \
0.50  0.510162  0.269053  0.308494  0.300554  0.294360  0.286980  0.280494   
0.55  0.489953  0.258690  0.290044  0.283933  0.278180  0.271426  0.266580    

我想通过遍历数据帧中的整个值来访问数据帧的相同位置.

And I want to access the same location of the dataframe by iterating over the whole values in the dataframe.

首先我尝试了 iterrows()

Firstly I tried iterrows()

i = 0
for index, row in df_one.iterrows():
    j= 0
    for item in row:
        print df_two(i,j)
        j= j+1
    i = i+1

但如您所知,我们无法访问:

but as you know we can not access like:

df_two(i,j)

所以我目前迷路了.或者我们可以通过索引名和列名访问数据吗?

So I am currently lost the way. Or could we access the data by index name and column name?

推荐答案

下面的代码还将使您能够在相同位置的两个数据帧上查找值.

Below code will also enable you to find values on both dataframes in same locations.

蟒蛇 2x

for i in range(0, len(df_one.index)):
    for j in range(0, len(df_one.columns)):
        print df_one.values[i,j],df_two.values[i,j],i,j

蟒蛇 3x

for i in range(0, len(df_one.index)):
    for j in range(0, len(df_one.columns)):
        print(df_one.values[i,j],df_two.values[i,j],i,j)

这篇关于Pandas:如何迭代两个格式完全相同的数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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