Ma0
Ma0

Reputation: 15204

How to zip two pandas dataframes

I have two Pandas dataframes like the ones below (sample code given here).

                   A         B         C         D
2000-01-01  0.298399 -0.738440 -0.505438  0.324951
2000-01-02  1.300686 -0.938543 -1.850977  0.868467

and

                   A         B         C         D
2000-01-03  0.328032  0.325845 -0.289175  0.393732
2000-01-04  0.091853 -1.450642 -0.544152  1.073248

and I would like to zip them so I can process their values. To clarify, I am trying to take two rows at a time — by zipping the corresponding rows together. I do not want to create a new df unless it would be more efficient that way.


I did the first thing that came to mind, i.e.,

for i, (x, y) in enumerate(zip(df1, df2)):
    print(i, x, y)

and expected to get something like:

0 (0.298399 -0.738440 -0.505438  0.324951) (0.328032  0.325845 -0.289175  0.393732)
1 (1.300686 -0.938543 -1.850977  0.868467) (0.091853 -1.450642 -0.544152  1.073248)

but what I got was:

0 A A
1 B B

How can one get the typical zip behavior when working with dataframes?

Surprisingly, I could not find a duplicate for my question since both this and this are asking something different.

Upvotes: 5

Views: 12287

Answers (1)

jezrael
jezrael

Reputation: 862611

You can convert DataFrames to numpy arrays:

for i, (x, y) in enumerate(zip(df1.values, df2.values)):
    print(i, x, y)

Your solution return columns names, becuse is processes like:

for i, (x, y) in enumerate(zip(df1.columns, df2.columns)):
    print(i, x, y)

Upvotes: 7

Related Questions