[Python] Pandas之sort_values isin使用技巧
2016-11-17 09:44
495 查看
1.在pandas的DataFrame中,我们经常需要根据某属性来选取指定条件的行,这时isin方法就特别有效。
这时假设我们选取A列中值为1的行,
2.pandas中的DataFrame如何按第一关键字,第二关键字对其进行排序,这里可以使用sort_values,老版本中为sort_index。
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
相关文章推荐
- python之pandas的使用:数据的导入技巧
- 深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法
- SAP ABAP 性能优化技巧 — 使用 ABAP “Sort” 取代 “Order By”
- 【Python基础】之对字典进行排序操作(sort by the values of dict)
- [Python]网络爬虫(五):urllib2的使用细节与抓站技巧
- PyCharm python IDE的使用技巧
- 深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法
- Python开发环境Wing IDE使用技巧之Blender的Python代码调试
- python技巧31[python中使用enum](转)
- python技巧31[python中使用enum]
- if ...__name__使用技巧总结 分类: python基础学习 python Module python 2013-11-01 14:51 262人阅读 评论(0) 收藏
- 【转帖】使用python爬虫抓站的一些技巧总结:进阶篇
- 使用python爬虫抓站的一些技巧总结:进阶篇
- python使用技巧
- Python splitlines使用技巧
- 【转帖】使用python爬虫抓站的一些技巧总结:进阶篇
- python技巧31[python中使用enum]
- 设置服务器[Python]网络爬虫(五):urllib2的使用细节与抓站技巧
- Python开发环境Wing IDE使用教程之matplotlib 2D绘图库代码调试技巧