Zhang18 - 1 year ago 113
Python Question

# Convert pandas dataframe elements to tuple

I have a dataframe:

``````>>> df = pd.DataFrame(np.random.random((3,3)))
>>> df
0         1         2
0  0.732993  0.611314  0.485260
1  0.935140  0.153149  0.065653
2  0.392037  0.797568  0.662104
``````

What is the easiest way for me convert each entry to a 2-tuple, with first element from the current dataframe, and 2nd element from the last columns (
`'2'`
)?

i.e. I want the final results to be:

``````                      0                    1                      2
0  (0.732993, 0.485260)  (0.611314, 0.485260)  (0.485260, 0.485260)
1  (0.935140, 0.065653)  (0.153149, 0.065653)  (0.065653, 0.065653)
2  (0.392037, 0.662104)  (0.797568, 0.662104)  (0.662104, 0.662104)
``````

As of pd version 0.20, you can use `df.transform`:

``````In [111]: df
Out[111]:
0  1  2
0  1  3  4
1  2  4  5
2  3  5  6

In [112]: df.transform(lambda x: list(zip(x, df[2])))
Out[112]:
0       1       2
0  (1, 4)  (3, 4)  (4, 4)
1  (2, 5)  (4, 5)  (5, 5)
2  (3, 6)  (5, 6)  (6, 6)
``````

Or, another solution using `df.apply`:

``````In [113]: df.apply(lambda x: list(zip(x, df[2])))
Out[113]:
0       1       2
0  (1, 4)  (3, 4)  (4, 4)
1  (2, 5)  (4, 5)  (5, 5)
2  (3, 6)  (5, 6)  (6, 6)
``````

You can also use dict comprehension:

``````In [126]: pd.DataFrame({i : df[[i, 2]].apply(tuple, axis=1) for i in df.columns})
Out[126]:
0       1       2
0  (1, 4)  (3, 4)  (4, 4)
1  (2, 5)  (4, 5)  (5, 5)
2  (3, 6)  (5, 6)  (6, 6)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download