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

  

 

posted @ 2018-09-07 11:40  路边的十元钱硬币  阅读(178)  评论(0编辑  收藏  举报