简体   繁体   English

如何按行值过滤此 pandas dataframe?

[英]How to filter this pandas dataframe by row’s value?

This is my pandas dataframe这是我的 pandas dataframe

print(df)

Name       Sale Value
ADAMSAE    1475000
BAKERDE    1412000
CLARKAE    1480450
DAVISDE    1483700
EVANSAE    1285000

Can anyone tell me how can I filter the row by its Name columns last two character of the string like this--- ADAMSAE BAKERDE by AE DE谁能告诉我如何通过名称列的字符串的最后两个字符过滤行,就像这样--- ADAMSAE BAKERDE by AE DE

Expected dataframe------预期数据框------

print(AE_df)
Name       Sale Value
ADAMSAE    1475000
CLARKAE    1480450
EVANSAE    1285000

and

print(DE_df)
Name       Sale Value
BAKERDE    1412000
DAVISDE    1483700

You can use df['Name'].str[-2:] .您可以使用df['Name'].str[-2:]

output: output:

0    AE
1    DE
2    AE
3    DE
4    AE
Name: Name, dtype: object

To make groups automatically:自动创建组:

d = dict(list(df.groupby(df['Name'].str[-2:])))

output: output:

{'AE':       Name  Sale Value
 0  ADAMSAE     1475000
 2  CLARKAE     1480450
 4  EVANSAE     1285000,
 'DE':       Name  Sale Value
 1  BAKERDE     1412000
 3  DAVISDE     1483700}

you could use endswith , like:你可以使用endswith ,比如:

df[df.Name.str.endswith('AE')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM