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【相关推荐】
以上就是在pandas的DataFrame中sort_values isin的使用实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号