[英]Python one-line boolean
Given df
给定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.其中,如果数字大于 15,则为 1,否则为 0。 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.但它给出了语法错误。
You can convert the booleans to ints like this:您可以像这样将布尔值转换为整数:
df_new = (df>15).astype(int)
You can apply the condition to the entire df all at once:您可以一次将条件应用于整个 df:
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.结果将是 boolean,但在您关心的所有方面都会表现为零和一。
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]
.至于您的原始表达式,三元运算符的形式[a if cond else b for x in iterable]
而过滤器看起来像[a for x in iterable if cond]
。 Notice where the if
goes in both cases.注意if
在这两种情况下的位置。
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]
Simply use where
function.只需使用where
即可。
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])
语法: numpy.where(condition[, x, y])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.