Reputation: 25
I have a PandaSeries with indicies that represent particular group and a value assigned to it
data = pd.Series(data=[12875.30, 12561.31, 12654.38, 13091.60, 13062.22, 13345.32], index=['A1','A2','B1','B2','C1','C2'])
data
A1 12875.30
A2 12561.31
B1 12654.38
B2 13091.60
C1 13062.22
C2 13345.32
What I am trying to achieve is to save the data for the B1, B2, C1 and C2 indicies and get rid of everything else using .filter method. The code below works, but only in case if I pass 1 attribute per time:
new_data = data.filter(like='B', axis=0)
new_data = data.filter(like='C', axis=0)
The question is how to combine those two lines into one using or | operator? I've tried many options, for example:
new_data = data.filter(like=('B' | 'C'), axis=0)
new_data = data.filter((like='B') | (like='C'), axis=0)
new_data = (data.filter(like='B', axis=0)) | (data.filter(like='C', axis=0))
new_data = ((data.filter(like='B', axis=0)) | (data.filter(like='C', axis=0)))
but everything I come up with results an error. Would appreciate any help. Thank you!
Upvotes: 1
Views: 58
Reputation: 16147
You could try using a regular expression:
data.filter(regex='B|C', axis=0)
Output
B1 12654.38
B2 13091.60
C1 13062.22
C2 13345.32
Upvotes: 1