1.在pandas的DataFrame中,我们经常需要根据某属性来选取指定条件的行,这时isin方法就特别有效。
- import pandas as pd
- df = pd.DataFrame([[1,2,3],[1,3,4],[2,4,3]],index = ['one','two','three'],columns = ['A','B','C'])
- print df
-
-
-
-
这时假设我们选取A列中值为1的行,
- mask = df['A'].isin([1])
- print mask
-
-
-
-
- print df[mask]
-
-
-
2.
pandas中的DataFrame如何按第一关键字,第二关键字对其进行排序
,这里可以使用sort_values
,老版本中为sort_index。
- import pandas as pd
- df = pd.DataFrame([[1,2,3],[2,3,4],[2,4,3],[1,3,7]],
- index = ['one','two','three','four'],columns = ['A','B','C'])
- print df
-
-
-
-
-
- df.sort_values(by=['A','B'],ascending=[0,1],inplace=True)
- print df
-
-
-
-
-