Home > Back-end >  Python one-line boolean
Python one-line boolean

Time:09-07

Given df

    col1
0    -12
1    -64
2     24
3      2
4      6
5     75
6      3
7     10
8     42
9     15
10    14

I'm trying to make something like:

df_new

    col1
0      0
1      0
2      1
3      0
4      0
5      1
6      0
7      0
8      1
9      0
10     0

Where, if the number is greater than 15, then it is 1 and 0 otherwise. I tried to do it this way:

df_new = [1 for x in df if (x<15) else 0]

But it gives a syntax error.

CodePudding user response:

You can convert the booleans to ints like this:

df_new = (df>15).astype(int)

CodePudding user response:

You can apply the condition to the entire df all at once:

new_df = df > 15

The result will be a boolean, but will behave as zeros and ones in all the ways that you care about.

As for your original expression, a ternary operator has the form [a if cond else b for x in iterable] while a filter looks like [a for x in iterable if cond]. Notice where the if goes in both cases.

CodePudding user response:

While others are right about using the features specific to dataframes, your understanding of list comprehension syntax is only a little bit off.

[1 if x < 15 else 0 for x in df]

Or if parentheses help you to see what's going on:

[(1 if x < 15 else 0) for x in df]

CodePudding user response:

Simply use where function.

import pandas as pd
import numpy as np
df=pd.DataFrame([-12,-64,24,2,6,75,3,10,42,15,14])
df=np.where(df>15,1,0)
df=pd.DataFrame(df)

Syntax : numpy.where(condition[, x, y])

  • Related