[英]Condition for an python panda dataframe using an List with range
How can I create an if conditions in a data frame that includes a list with a range between the elements? 如何在包含一个元素之间具有范围的列表的数据框中创建if条件? I want a new smaller data frame that is in a range of certain values.
我想要一个在某些值范围内的新的较小数据框。 Like
x
element of [2,4]
and save it in a new data frame. 类似于
[2,4]
x
元素,并将其保存在新的数据框中。
For example: 例如:
x y
1 4
2 7
3 8
4 11
5 -15
6 13
My new table should be: 我的新表应该是:
df[df[x] <= [2,4]]
x y
2 7
3 8
4 11
I tried the following: 我尝试了以下方法:
df[df['time']<=[1212,1220].to_csv(path_or_buf=DATAFILE_OUTPUT, index=True, sep='\t', columns=out_columns, decimal='.')
But got a syntax error: 但是出现语法错误:
SyntaxError: unexpected EOF while parsing
Use Series.between
for boolean mask: 使用
Series.between
作为布尔掩码:
df = df[df['x'].between(2,4)]
print (df)
x y
1 2 7
2 3 8
3 4 11
It working same like 2 conditions chained by &
for bitwise AND
: 它的工作原理与2个条件相同,由
&
链接按位AND
:
df = df[(df['x'] >= 2) & (df['x'] <= 4)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.