• 15-numpy笔记-莫烦pandas-3


    代码

    import pandas as pd
    import numpy as np
    
    dates = pd.date_range('20130101', periods=6)
    df=pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])
    
    # 行数,列数,赋值
    df.iloc[1,2] = 1111
    df.loc['20130101','B'] = 2222
    print('-1-')
    print(df)
    
    df[df.A>4] = 0
    print('-2-')
    print(df)
    
    df.A[df.A>4] = 0
    print('-3-')
    print(df)
    
    # 添加列
    df['F'] = np.nan
    print('-4-')
    print(df)
    
    df['E'] = pd.Series([1,2,3,4,5,6],index=pd.date_range('20130101',periods=6))
    print('-5-')
    print(df)
    

      

    输出

    -1-
                 A     B     C   D
    2013-01-01   0  2222     2   3
    2013-01-02   4     5  1111   7
    2013-01-03   8     9    10  11
    2013-01-04  12    13    14  15
    2013-01-05  16    17    18  19
    2013-01-06  20    21    22  23
    -2-
                A     B     C  D
    2013-01-01  0  2222     2  3
    2013-01-02  4     5  1111  7
    2013-01-03  0     0     0  0
    2013-01-04  0     0     0  0
    2013-01-05  0     0     0  0
    2013-01-06  0     0     0  0
    -3-
                A     B     C  D
    2013-01-01  0  2222     2  3
    2013-01-02  4     5  1111  7
    2013-01-03  0     0     0  0
    2013-01-04  0     0     0  0
    2013-01-05  0     0     0  0
    2013-01-06  0     0     0  0
    -4-
                A     B     C  D   F
    2013-01-01  0  2222     2  3 NaN
    2013-01-02  4     5  1111  7 NaN
    2013-01-03  0     0     0  0 NaN
    2013-01-04  0     0     0  0 NaN
    2013-01-05  0     0     0  0 NaN
    2013-01-06  0     0     0  0 NaN
    -5-
                A     B     C  D   F  E
    2013-01-01  0  2222     2  3 NaN  1
    2013-01-02  4     5  1111  7 NaN  2
    2013-01-03  0     0     0  0 NaN  3
    2013-01-04  0     0     0  0 NaN  4
    2013-01-05  0     0     0  0 NaN  5
    2013-01-06  0     0     0  0 NaN  6
    

      

  • 相关阅读:
    Retrofit源码分析
    Android异步消息机制
    崩溃bug日志总结3
    单例模式
    总结(第一段)
    mysql日期类型比较
    mysql记录(一)
    JSONObject/JSONArray的区别
    java 正则表达式(Pattern ,Matcher)的使用
    javaweb开发中的权限管理的方法
  • 原文地址:https://www.cnblogs.com/alexYuin/p/9603643.html
Copyright © 2020-2023  润新知