我有以下数据框,其中包含用户名和金额,我只需要选择用户名为大写且金额没有连字符"-"的值
userid username amount
0 MAX 1.00
1 will -
2 JOHN 5.22
3 sara -
4 Nell 1.121
我需要输出数据帧作为
MAX 1.00
JOHN 5.22
您可以使用 is.upper(( 来检查 'username' 列是否为大写:
df[df.username.str.isupper()]
并且您的金额不应是字符串"-",如下所示:
df[df.amount!='-']
合并后为:
df.loc[(df.amount!='-')&(df.username.str.isupper())]
仅将Series.str.isupper
用于测试的大写字符串,Series.ne
表示测试不相等,按位AND
按&
串,并在boolean indexing
中过滤:
df = df[df['username'].str.isupper() & df['amount'].ne('-')]
或:
df = df[df['username'].str.contains('^[A-Z]+$') & df['amount'].ne('-')]
print (df)
userid username amount
0 0 MAX 1.00
2 2 JOHN 5.22