2

How to create new column and set values which are results of mapping this dataframe with another object for instance list of lists python?

I have pandas dataframe:

{'a': [15,10,11,9,7,8], 'b':['smth','smth','smth','smth', 'smth', 'smth'].

And list of list:

[[15,10], [11], [9,7,8]]

I want create new column in my dataframe, which will be contain 3 big classes like in my list of lists.

I mean, I want to get this:

{'a': [15,10,11,9,7,8], 'b':['smth','smth','smth','smth', 'smth', 'smth',
'new_column': [0,0,1,2,2,2]}
1
  • Please show us what you've tried so far; also I'm not sure I understand what you're trying to do, seems like you can just use dataframe['new_column'] = ... from what you've shown so far Commented Jul 27, 2017 at 11:04

2 Answers 2

3

You can use map by dict created by dict comprehension, values of list has to be unique:

df = pd.DataFrame({'a': [15,10,11,9,7,8], 'b':['smth','smth','smth','smth', 'smth', 'smth']})

L = [[15,10], [11], [9,7,8]]
#https://stackoverflow.com/q/45349225/2901002
d = { v : i for i,vs in enumerate(L) for v in vs}
#alternative solution
#d = {v: i for i in range(len(L)) for v in L[i]}
print (d)
{7: 2, 8: 2, 9: 2, 10: 0, 11: 1, 15: 0}

df['new_column'] = df['a'].map(d)
print (df)
    a     b  new_column
0  15  smth           0
1  10  smth           0
2  11  smth           1
3   9  smth           2
4   7  smth           2
5   8  smth           2
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you! This helps me =))
Unfortunately only one solution can be accepted, another solution was better?
You have my vote because I think your solution is clean. +1
@cᴏʟᴅsᴘᴇᴇᴅ - Thank you. I think your solution is overcomplicated. And also list comprehension is the best avoid in pandas with some exceptions.
Thank you :) Haha I am a pythonista. However, I do know the basics of other languages (c, java, full stack, apex, etc). My top answer is essentially how to split a number into two parts. It's so basic that I am embarrassed it is at the top. I have written hundreds of better answers. xD Started pandas recently as I am going into a data sciences masters course next month. It will help,
|
0

You can use np.where in a list comprehension:

In [926]: import itertools

In [927]: l = np.array(list(itertools.zip_longest(*[[15,10], [11], [9,7,8]], fillvalue=0))).T

In [928]: df['new'] = [np.where(l == i)[0][0] for i in df.a.values]

In [929]: df
Out[929]: 
    a     b  new
0  15  smth    0
1  10  smth    0
2  11  smth    1
3   9  smth    2
4   7  smth    2
5   8  smth    2

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.