我希望能够保留有关<= 25岁且<= 200磅的球员的信息。
这是我所做的:
代码1:
# importing pandas as pd
import pandas as pd
data = {'Nage':['John Doe', 'Billy Bob', 'Franky Bill'],
'Age':['27','25','27'],
'Weight':['210','185','220']}
df = pd.DataFrame(data)
df['Age']=df['Age'].astype(str).astype(float)
df['Weight']=df['Weight'].astype(str).astype(float)
# Visualize the dataframe
print(df.head())
# Print the shape of the dataframe
print(df.shape)
预期的输出1:
Nage Age Weight
0 John Doe 27.0 210.0
1 Billy Bob 25.0 185.0
2 Franky Bill 27.0 220.0
(3, 3)
代码2:
# Filter all rows for which the player's
# age is less than or equal to 25 and weight less than or equal to 200 lbs
df_filtered = df[df['Age'] <= 25]
df_filtered2 = df_filtered[df_filtered['Weight'] <= 200]
# Print the new dataframe
print(df_filtered.head())
# Print the shape of the dataframe
print(df_filtered.shape)
预期的输出2:
Nage Age Weight
1 Billy Bob 25.0 185.0
(1, 3)
有人能想到一种更简单的方法吗?可能带有“和”或“ |”
我尝试过
df.drop((df[df['Age'] >= 25.0 | df['Weight'] >= 200.0]).index, inplace=True)
但是我得到这个错误:
Cannot perform 'ror_' with a dtyped [float64] array and scalar of type [bool]
如果我将|
替换为or
,则会出现此错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
答案 0 :(得分:0)
这是解决方案:您只需要在两个子条件两边加上括号:
df.drop((df[(df['Age'] >= 25.0) | (df['Weight'] >= 200.0)]).index, inplace=True)
或者,根据用户应用程序,他们可以使用“&”或“ |”。我发现“&”运算符在我的情况下效果最好,因此它指定必须满足两个条件才能删除行。