[Python] Slice the data with pandas

For example we have dataframe like this:

                   SPY        AAPL         IBM        GOOG         GLD
2017-01-03  222.073914  114.311760  160.947433  786.140015  110.470001
2017-01-04  223.395081  114.183815  162.940125  786.900024  110.860001
2017-01-05  223.217606  114.764473  162.401047  794.020020  112.580002
2017-01-06  224.016220  116.043915  163.200043  806.150024  111.750000
2017-01-09  223.276779  117.106812  161.390244  806.650024  112.669998
...

 

Now we only we want to get highlighted part:

                   SPY        AAPL         IBM        GOOG         GLD
2017-01-03  222.073914  114.311760  160.947433  786.140015  110.470001
2017-01-04  223.395081  114.183815  162.940125  786.900024  110.860001
2017-01-05  223.217606  114.764473  162.401047  794.020020  112.580002
2017-01-06  224.016220  116.043915  163.200043  806.150024  111.750000
2017-01-09  223.276779  117.106812  161.390244  806.650024  112.669998

We can use Dataframe.ix[] method to get date related index data from the list.

复制代码
if __name__ == '__main__':
    data=get_data()
    data=data.ix['2017-12-01':'2017-12-15', ['IBM', 'GOOG']]    
    print(data)
    """
                       IBM         GOOG
    2017-12-01  154.759995  1010.169983
    2017-12-04  156.460007   998.679993
    2017-12-05  155.350006  1005.150024
    2017-12-06  154.100006  1018.380005
    2017-12-07  153.570007  1030.930054
    2017-12-08  154.809998  1037.050049
    2017-12-11  155.410004  1041.099976
    2017-12-12  156.740005  1040.479980
    2017-12-13  153.910004  1040.609985
    2017-12-15  152.500000  1064.189941
    """
复制代码

 

posted @   Zhentiw  阅读(314)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-12-17 [Angular Router] Lazy loading Module with Auxiliary router
2016-12-17 [Now] Configure secrets and environment variables with Zeit’s Now
2015-12-17 [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
2015-12-17 [Redux] Composition with Objects
2014-12-17 [Javascript] Function scope
2014-12-17 [AngularJS] ui-router: named views
2014-12-17 [AngularJS] ui-router: Abstract States
点击右上角即可分享
微信分享提示