[英]select range of values for all columns in pandas dataframe
I have a dataframe 'DF', part of which looks like this:我有一个 dataframe 'DF',其中一部分看起来像这样:
I want to select only the values between 0 and 0.01, to form a new dataframe(with blanks where the value was over 0.01)我想 select 只有 0 和 0.01 之间的值,以形成一个新的数据帧(值超过 0.01 的空白)
To do this, i tried:为此,我尝试了:
similarity = []
for x in DF:
similarity.append([DF[DF.between(0, 0.01).any(axis=1)]])
simdf = pd.DataFrame(similarity)
simdf.to_csv("similarity.csv")
However, i get the error AttributeError: 'DataFrame' object has no attribute 'between'但是,我收到错误 AttributeError: 'DataFrame' object has no attribute 'between'
How do i select a range of values and create a new data frame with these?我如何 select 一系列值并使用这些值创建一个新的数据框?
Just do the two comparisons:只需做两个比较:
df_new = df[(df>0) & (df<0.01)]
Example:例子:
import pandas as pd
df = pd.DataFrame({"a":[0,2,4,54,56,4],"b":[4,5,7,12,3,4]})
print(df[(df>5) & (df<33)])
a b
0 NaN NaN
1 NaN NaN
2 NaN 7.0
3 NaN 12.0
4 NaN NaN
5 NaN NaN
If want blank string instead of NaN
:如果想要空白字符串而不是
NaN
:
df[(df>5) & (df<33)].fillna("")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.