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 b c
# one 1 2 3
# two 1 3 4
# three 2 4 3
这时假设我们选取a列中值为1的行,
mask = df['a'].isin([1]) #括号中必须为list
print mask
# one true
# two true
# three false
# name: a, dtype: bool
print df[mask]
# a b c
# one 1 2 3
# two 1 3 4
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
# a b c
# one 1 2 3
# two 2 3 4
# three 2 4 3
# four 1 3 7
df.sort_values(by=['a','b'],ascending=[0,1],inplace=true)
print df
# a b c
# two 2 3 4
# three 2 4 3
# one 1 2 3
# four 1 3 7
【相关推荐】
1. 详解python中的sort()使用方法
2. 详解python中使用values()的实例教程
3. 分享python中sort的使用方法实例
以上就是在pandas的dataframe中sort_values isin的使用实例的详细内容。