Jack
Jack

Reputation: 1754

How to exclude some string patterns when using filter on pandas?

dataframe

df.columns=['ipo_date','l2y_gg_date','l1k_kk_date']

Goal

Try

Upvotes: 0

Views: 199

Answers (1)

Henry Ecker
Henry Ecker

Reputation: 35676

Try a negative lookbehind:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(1, 21).reshape((5, 4)),
                  columns=['ipo_date', 'l2y_gg_date', 'l1k_kk_date', 'other'])

filtered = df.filter(regex=r'(?<!ipo)_date')
print(filtered)

Sample df:

   ipo_date  l2y_gg_date  l1k_kk_date  other
0         1            2            3      4
1         5            6            7      8
2         9           10           11     12
3        13           14           15     16
4        17           18           19     20

filtered:

   l2y_gg_date  l1k_kk_date
0            2            3
1            6            7
2           10           11
3           14           15
4           18           19

Upvotes: 2

Related Questions