For object
data I can map two columns into a third, (object
) column of tuples
>>> import pandas as pd
>>> df = pd.DataFrame([["A","b"], ["A", "a"],["B","b"]])
>>> df
0 1
0 A b
1 A a
2 B b
>>> df.apply(lambda row: (row[0], row[1]), axis=1)
0 (A, b)
1 (A, a)
2 (B, b)
dtype: object
(see also Pandas: How to use apply function to multiple columns).
However, when I try to do the same thing with numerical columns
>>> df2 = pd.DataFrame([[10,2], [10, 1],[20,2]])
df2.apply(lambda row: (row[0], row[1]), axis=1)
0 1
0 10 2
1 10 1
2 20 2
so instead of a series of pairs (i.e. [(10,2), (10,1), (20,2)]
) I get a DataFrame
.
How can I force pandas
to actually get a series of pairs? (Preferably, doing it nicer than converting to string and then parsing.)