DataFrame.filter(items = None,like=None,regex=None,axis=None)
例子:
>>> df one two three mouse 1 2 3 rabbit 4 5 6
>>> # select columns by name >>> df.filter(items=['one', 'three']) one three mouse 1 3 rabbit 4 6
>>> # select columns by regular expression >>> df.filter(regex='e$', axis=1) one three mouse 1 3 rabbit 4 6
>>> # select rows containing 'bbi' >>> df.filter(like='bbi', axis=0) one two three rabbit 4 5 6
pandas的map映射()
import pandas as pd
# s = pd.DataFrame({'one':[1,2,3],'two':[4,5,6],'three':[7,8,9]},dtype='category')
s = pd.DataFrame({'one':[1,1,2,2,1],'two':[1,2,3,4,5]},dtype='category')
s['one'] = s['one'].map({1:'a',2:'b'})
print(s)
输出:
one two
0 a 1
1 a 2
2 b 3
3 b 4
4 a 5