15-numpy笔记-莫烦pandas-3
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | -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 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步