Pandas: How could I iterate two dataframes which have exactly same format?
pandas, python
Solution
Below code will also enable you to find values on both dataframes in same locations.
python 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
python 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)
Problem
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. 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. 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?