Jason Strimpel
Jason Strimpel

Reputation: 15496

Map List of Tuples to New Column

Suppose I have a pandas.DataFrame:

In [76]: df
Out[76]: 
          a         b  c
0 -0.685397  0.845976  w
1  0.065439  2.642052  x
2 -0.220823 -2.040816  y
3 -1.331632 -0.162705  z

Suppose I have a list of tuples:

In [78]: tp
Out[78]: [('z', 0.25), ('y', 0.33), ('x', 0.5), ('w', 0.75)]

I would like to map tp do df such that the the second element in each tuple lands in a new column that corresponds with the row matching the first element in each tuple.

The end result would look like this:

In [87]: df2
Out[87]: 
          a         b  c   new
0 -0.685397  0.845976  w  0.75
1  0.065439  2.642052  x  0.50
2 -0.220823 -2.040816  y  0.33
3 -1.331632 -0.162705  z  0.25

I've tried using lambdas, pandas.applymap, pandas.map, etc but cannot seem to crack this one. So for those that will point out I have not actually asked a question, how would I map tp do df such that the the second element in each tuple lands in a new column that corresponds with the row matching the first element in each tuple?

Upvotes: 3

Views: 2704

Answers (2)

EdChum
EdChum

Reputation: 394269

You need to turn your list of tuples into a dict which is ridiculously easy to do in python, then call map on it:

In [4]:

df['new'] = df['c'].map(dict(tp))
df
Out[4]:
              a         b  c   new
index                             
0     -0.685397  0.845976  w  0.75
1      0.065439  2.642052  x  0.50
2     -0.220823 -2.040816  y  0.33
3     -1.331632 -0.162705  z  0.25

The docs for map show that that it takes as a function arg a dict, series or function.

applymap takes a function as an arg but operates element wise on the whole dataframe which is not what you want to do in this case.

The online docs show how to apply an operation element wise, as does the excellent book

Upvotes: 4

James Paul Shaulis
James Paul Shaulis

Reputation: 111

Does this example help?

class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

>>> d = {'col1': ts1, 'col2': ts2}
>>> df = DataFrame(data=d, index=index)
>>> df2 = DataFrame(np.random.randn(10, 5))
>>> df3 = DataFrame(np.random.randn(10, 5),
... columns=['a', 'b', 'c', 'd', 'e'])

Upvotes: -1

Related Questions