• pandas取值


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/5/24 15:03
    # @Author  : zhang chao
    # @File    : s.py
    from scipy import linalg as lg
    #按标签选择
    #通过标签选择多轴
    
    import pandas as pd
    import numpy as np
    
    dates = pd.date_range('20170101', periods=6)
    df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
    print("df:")
    print(df)
    print('-'*50)
    #通过索引选择
    print("df.loc[:,['A','B']]") print(df.loc[:,['A','B']]) #显示标签切片,包括两个端点 print('-'*50) print("df.loc['20170102':'20170104',['A','B']]") print(df.loc['20170102':'20170104',['A','B']]) print('-'*50) #获得标量值 获取m行n列的单个数据值 print("df.loc[dates[0],'A'") print(df.loc[dates[0],'A']) #快速访问标量(等同于先前的方法) print('-'*50) print("df.at[dates[0],'A']") print(df.at[dates[0],'A'])#at快速访问单个值;loc访问多指 #通过传递的整数的位置选择 通过下标选择 print("df.iloc[3]") print(df.iloc[3]) print('-'*50) #通过整数切片,类似于numpy/python print("df.iloc[3:5,0:2]") print(df.iloc[3:5,0:2])#切片 #通过整数位置的列表,类似于numpy/python样式 print("df.iloc[[1,2,4],[0,2]]") print(df.iloc[[1,2,4],[0,2]])#列表 print("df.iloc[1:3,:]") print(df.iloc[1:3,:]) print("df.iloc[:,1:3]") print(df.iloc[:,1:3]) print("df.iloc[1,1]") print(df.iloc[1,1]) #要快速访问标量(等同于先前的方法) print("print(df.iat[1,1])") print(df.iat[1,1]) #布尔索引 #使用单列的值来选择数据 print("df[df.A > 0]") print(df[df.A > 0][df.B<0])#多条件选择 print("df[df > 0]") print(df[df > 0])#从满足布尔条件的DataFrame中选择值 #使用isin()方法进行过滤 df2 = df.copy() df2['E'] = ['one', 'one','two','three','four','three'] print("df2") print(df2) print("============= start to filter =============== ") print("isin") print(df2[df2['E'].isin(['two','four'])])

    D:Downloadpython3python3.exe D:/Download/pycharmworkspace/s.py
    df:
    A B C D
    2017-01-01 -1.353900 -0.737163 -0.266858 -0.219116
    2017-01-02 -2.328935 0.297892 0.244013 0.331435
    2017-01-03 0.442864 -1.837813 -0.523082 -1.058623
    2017-01-04 -2.117530 -0.480186 0.174002 -0.197551
    2017-01-05 -0.312444 -0.958863 0.004229 -0.998425
    2017-01-06 0.957020 -0.147027 0.125730 -0.643826
    --------------------------------------------------
    df.loc[:,['A','B']] #loc为原始索引 用键索引 字符索引
    A B
    2017-01-01 -1.353900 -0.737163
    2017-01-02 -2.328935 0.297892
    2017-01-03 0.442864 -1.837813
    2017-01-04 -2.117530 -0.480186
    2017-01-05 -0.312444 -0.958863
    2017-01-06 0.957020 -0.147027
    --------------------------------------------------
    df.loc['20170102':'20170104',['A','B']]
    A B
    2017-01-02 -2.328935 0.297892
    2017-01-03 0.442864 -1.837813
    2017-01-04 -2.117530 -0.480186
    --------------------------------------------------
    df.loc[dates[0],'A'
    -1.3539004392106717
    --------------------------------------------------
    df.at[dates[0],'A']#at快速取值
    -1.3539004392106717

    --------------------------------------------------
    df.iloc[3]#iloc为数字索引
    A -2.117530
    B -0.480186
    C 0.174002
    D -0.197551
    Name: 2017-01-04 00:00:00, dtype: float64
    --------------------------------------------------
    df.iloc[3:5,0:2]
    A B
    2017-01-04 -2.117530 -0.480186
    2017-01-05 -0.312444 -0.958863
    --------------------------------------------------
    df.iloc[[1,2,4],[0,2]]
    A C
    2017-01-02 -2.328935 0.244013
    2017-01-03 0.442864 -0.523082
    2017-01-05 -0.312444 0.004229
    --------------------------------------------------
    df.iloc[1:3,:]
    A B C D
    2017-01-02 -2.328935 0.297892 0.244013 0.331435
    2017-01-03 0.442864 -1.837813 -0.523082 -1.058623
    --------------------------------------------------
    df.iloc[:,1:3]
    B C
    2017-01-01 -0.737163 -0.266858
    2017-01-02 0.297892 0.244013
    2017-01-03 -1.837813 -0.523082
    2017-01-04 -0.480186 0.174002
    2017-01-05 -0.958863 0.004229
    2017-01-06 -0.147027 0.125730
    --------------------------------------------------
    df.iloc[1,1]
    0.29789175201181145
    --------------------------------------------------
    print(df.iat[1,1])#iat快速数字索引取值
    0.29789175201181145
    --------------------------------------------------
    df[df.A > 0]#按照A列的元素大于0 进行筛选取值
    A B C D
    2017-01-03 0.442864 -1.837813 -0.523082 -1.058623
    2017-01-06 0.957020 -0.147027 0.125730 -0.643826
    --------------------------------------------------
    df[df > 0]#保留数据大于0的元素,费大于0的元素为NaN
    A B C D
    2017-01-01 NaN NaN NaN NaN
    2017-01-02 NaN 0.297892 0.244013 0.331435
    2017-01-03 0.442864 NaN NaN NaN
    2017-01-04 NaN NaN 0.174002 NaN
    2017-01-05 NaN NaN 0.004229 NaN
    2017-01-06 0.957020 NaN 0.125730 NaN
    --------------------------------------------------
    df2
    A B C D E
    2017-01-01 -1.353900 -0.737163 -0.266858 -0.219116 one
    2017-01-02 -2.328935 0.297892 0.244013 0.331435 one
    2017-01-03 0.442864 -1.837813 -0.523082 -1.058623 two
    2017-01-04 -2.117530 -0.480186 0.174002 -0.197551 three
    2017-01-05 -0.312444 -0.958863 0.004229 -0.998425 four
    2017-01-06 0.957020 -0.147027 0.125730 -0.643826 three
    --------------------------------------------------
    ============= start to filter ===============
    isin

    df2['E'].isin(['two','four'])
    df2[df2['E'].isin(['two','four'])]

    #如果E列中的元素在 isin里面 则获取到值
    A B C D E
    2017-01-03 0.442864 -1.837813 -0.523082 -1.058623 two
    2017-01-05 -0.312444 -0.958863 0.004229 -0.998425 four

    
    

    Process finished with exit code 0

     
  • 相关阅读:
    模块二 GO语言进阶技术-panic函数、recover函数以及defer语句(上)
    模块二 GO语言进阶技术-错误处理(下)
    模块二 GO语言进阶技术-错误处理(上)
    模块二 GO语言进阶技术-if语句、for语句和switch语句
    模块二 GO语言进阶技术-GO语句及其执行规则(下)
    模块二 GO语言进阶技术-go语句及其执行规则(上)
    模块二 GO语言进阶技术-关于指针的有限操作
    模块二 GO语言进阶技术-接口类型的合理运用
    模块二 GO语言进阶技术-结构体及其方法的使用法门
    模块二 GO语言进阶技术-使用函数的正确姿势
  • 原文地址:https://www.cnblogs.com/ggzhangxiaochao/p/9090250.html
Copyright © 2020-2023  润新知