Comparison of a Dataframe column values with a list

Viewed 45867

Consider this Dataframe:

df = pd.DataFrame({'A': [1, 1, 2, 2, 3, 3],
                   'B': [10, 15, 20, 25, 30,35],
                   'C': [100, 150, 200, 250, 300, 350]})

This is the code to get values of column C, where it is the first row of each group (Column A):

firsts = df.groupby('A').first()['C']

So first will be: (100, 200, 300).

Now I want to add new column which it will be 1 if value of column C for row is in firsts otherwise it will be 0.

A B C D
1 10 100 1
1 15 150 0
2 20 200 1
2 25 250 0
3 30 300 1
3 35 350 0

I used this:

df['D'] = df['C'].apply(lambda x: 1 if x in firsts else 0)

But the output is:

A B C D
1 10 100 0
1 15 150 0
2 20 200 0
2 25 250 0
3 30 300 0
3 35 350 0

I appreciate if anyone explain why my solution is wrong and what is actual solution to this problem?

4 Answers

firsts is pandas series , so when we use in to search for value then it will search that value in index list to solve this we can convert firsts to list or array

%timeit df['D'] = df['C'].apply(lambda x: 1 if x in firsts.values else 0)

314 µs ± 17.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

or

%timeit df['D'] = df['C'].apply(lambda x: 1 if x in list(firsts) else 0)

301 µs ± 11.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

or

%timeit df['D'] = list(map(lambda x: 1 if x in list(firsts) else 0,list(df['C'])))

27.6 µs ± 1.02 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Related