This is my 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
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
CodePudding user response:
You can use df['Name'].str[-2:]
.
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:
{'AE': Name Sale Value
0 ADAMSAE 1475000
2 CLARKAE 1480450
4 EVANSAE 1285000,
'DE': Name Sale Value
1 BAKERDE 1412000
3 DAVISDE 1483700}
CodePudding user response:
you could use endswith, like:
df[df.Name.str.endswith('AE')]