2

I need some help with forming a new variable in a dataframe I have a very large dataset were i want to make a new variable from existing data in another column. Here is an example.

>>> df
   A  B   
0  a  1  
1  b  2  
2  c  3  
3  a  5  
4  d  6  

I wwant to create a new column with the values a and c in A and name them ac, and the b and d and name them bd Final result should look like. Kind of grouping them from A and give the group a new name in a new column.

>>> df
   A  B   C
0  a  1  ac
1  b  2  bd
2  c  3  ac
3  a  5  ac
4  d  6  bd

/Jonas

2
  • how are the mappings in C determined? Commented Jun 29, 2013 at 13:46
  • a or c should result in ac in C column...that is a and c should be set as beloning to a group called ac.. Commented Jun 29, 2013 at 14:25

1 Answer 1

1
In [4]: mapper = dict(a = 'ac', b = 'bd', c = 'ac', d = 'bd')

In [5]: df['C'] = [ mapper[x] for x in df['A'] ]

In [6]: df
Out[6]: 
   A  B   C
0  a  1  ac
1  b  2  bd
2  c  3  ac
3  a  5  ac
4  d  6  bd
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.