选择、赋值与切片
df:
A B C D
2018-10-01 -0.244803 0.241785 1.198658 -0.873602
2018-10-02 1.196060 -2.049168 -2.107360 -1.910530
2018-10-03 -1.842837 0.276093 0.143275 0.613543
2018-10-04 0.620139 -0.956985 0.031955 1.388788
2018-10-05 0.653898 -1.887507 0.037620 -0.337932
2018-10-06 -0.566056 0.742715 -0.363279 0.235169
print(df['A'])
2018-10-01 -0.244803
2018-10-02 1.196060
2018-10-03 -1.842837
2018-10-04 0.620139
2018-10-05 0.653898
2018-10-06 -0.566056
Freq: D, Name: A, dtype: float64
print(df[0:3])#包左不包右
A B C D
2018-10-01 -0.244803 0.241785 1.198658 -0.873602
2018-10-02 1.196060 -2.049168 -2.107360 -1.910530
2018-10-03 -1.842837 0.276093 0.143275 0.613543
print(df['20181002':'20181004'])#包左包右
A B C D
2018-10-02 1.196060 -2.049168 -2.107360 -1.910530
2018-10-03 -1.842837 0.276093 0.143275 0.613543
2018-10-04 0.620139 -0.956985 0.031955 1.388788
print(df.loc[dates[0]])#标签获取横截面
A -0.244803
B 0.241785
C 1.198658
D -0.873602
Name: 2018-10-01 00:00:00, dtype: float64
print(df.loc[:,['A','B']])
A B
2018-10-01 -0.244803 0.241785
2018-10-02 1.196060 -2.049168
2018-10-03 -1.842837 0.276093
2018-10-04 0.620139 -0.956985
2018-10-05 0.653898 -1.887507
2018-10-06 -0.566056 0.742715
print(df.loc['20181002':'20181004',['A','B']])#包左包右
A B
2018-10-02 1.196060 -2.049168
2018-10-03 -1.842837 0.276093
2018-10-04 0.620139 -0.956985
print(df.loc['20181002',['A','B']])
A 1.196060
B -2.049168
Name: 2018-10-02 00:00:00, dtype: float64
print(df.loc[dates[0],'A'])
print(df.at[dates[0],'A'])#同上
-0.2448032167642576
-0.2448032167642576
print(df.iloc[3])#行索引为3
A 0.620139
B -0.956985
C 0.031955
D 1.388788
Name: 2018-10-04 00:00:00, dtype: float64
print(df.iloc[3:5,0:2])
A B
2018-10-04 0.620139 -0.956985
2018-10-05 0.653898 -1.887507
print(df.iloc[[1,2,4],[0,2]])#行124列02的索引
A C
2018-10-02 1.196060 -2.107360
2018-10-03 -1.842837 0.143275
2018-10-05 0.653898 0.037620
print(df[df.A > 0])
A B C D
2018-10-02 1.196060 -2.049168 -2.107360 -1.910530
2018-10-04 0.620139 -0.956985 0.031955 1.388788
2018-10-05 0.653898 -1.887507 0.037620 -0.337932
print(df[df > 0])
A B C D
2018-10-01 NaN 0.241785 1.198658 NaN
2018-10-02 1.196060 NaN NaN NaN
2018-10-03 NaN 0.276093 0.143275 0.613543
2018-10-04 0.620139 NaN 0.031955 1.388788
2018-10-05 0.653898 NaN 0.037620 NaN
2018-10-06 NaN 0.742715 NaN 0.235169
df2 = df.copy()
df2['E'] = ['one', 'one', 'two', 'three', 'four', 'three']
print(df2[df2['E'].isin(['two','four'])])
A B C D E
2018-10-03 -1.842837 0.276093 0.143275 0.613543 two
2018-10-05 0.653898 -1.887507 0.037620 -0.337932 four
s1 = pd.Series([1,2,3,4,5,6],index=pd.date_range('20181002',periods=6))
df['F'] = s1
df.at[dates[0],'A'] = 0#标签赋值
df.iat[0, 1] = 0#位置赋值
df.loc[:,'D'] = np.array([5] * len(df))
print(df)
A B C D F
2018-10-01 0.000000 0.000000 1.198658 5 NaN
2018-10-02 1.196060 -2.049168 -2.107360 5 1.0
2018-10-03 -1.842837 0.276093 0.143275 5 2.0
2018-10-04 0.620139 -0.956985 0.031955 5 3.0
2018-10-05 0.653898 -1.887507 0.037620 5 4.0
2018-10-06 -0.566056 0.742715 -0.363279 5 5.0
df2 = df.copy()
print(df2)
A B C D F
2018-10-01 0.000000 0.000000 1.198658 5 NaN
2018-10-02 1.196060 -2.049168 -2.107360 5 1.0
2018-10-03 -1.842837 0.276093 0.143275 5 2.0
2018-10-04 0.620139 -0.956985 0.031955 5 3.0
2018-10-05 0.653898 -1.887507 0.037620 5 4.0
2018-10-06 -0.566056 0.742715 -0.363279 5 5.0
df2[df2 > 0] = -df2
print(df2)
A B C D F
2018-10-01 0.000000 0.000000 -1.198658 -5 NaN
2018-10-02 -1.196060 -2.049168 -2.107360 -5 -1.0
2018-10-03 -1.842837 -0.276093 -0.143275 -5 -2.0
2018-10-04 -0.620139 -0.956985 -0.031955 -5 -3.0
2018-10-05 -0.653898 -1.887507 -0.037620 -5 -4.0
2018-10-06 -0.566056 -0.742715 -0.363279 -5 -5.0
本文接上篇:pandas学习(一)