Numpy & Pandas (莫烦 Python 数据处理教程)-Pandas学习笔记(3)-设置值

Pandas学习笔记(3)-设置值

教程视频为B站莫烦 Python 数据处理教程 第13课程

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[2,2]=1111
df.loc['20130101','B'] = 2222
df.B[df.A>0] = 0
df['F'] = np.nan   #增加一列
df['E'] = pd.Series([1,2,3,4,5,6],index=pd.date_range('20130101',periods=6))       #增加一列,注意index(列)需要与原序列对齐
print(df)

#out

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

你可能感兴趣的:(莫烦Pandas学习笔记)