Python—-Pandas
JSON:JavaScript 对象表示法(JavaScript Object Notation)。
JSON 是存储和交换文本信息的语法。类似 XML。
JSON 比 XML 更小、更快,更易解析。
1.json的语法规则
import numpy as np
import pandas as pd
dates=pd.date_range('20171119',periods=6)
df=pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['A','B','C','D'])
print(df)
输出
A B C D
2017-11-19 0 1 2 3
2017-11-20 4 5 6 7
2017-11-21 8 9 10 11
2017-11-22 12 13 14 15
2017-11-23 16 17 18 19
2017-11-24 20 21 22 23
print(df['A'])
2017-11-19 0
2017-11-20 4
2017-11-21 8
2017-11-22 12
2017-11-23 16
2017-11-24 20
print(df.A)#作用同上
``
`
print(df.loc[‘20171121’])
A 8
B 9
C 10
D 11
Name: 2017-11-21 00:00:00, dtype: int32
print(df.loc[‘20171119’,[‘A’]])
A 0
Name: 2017-11-19 00:00:00, dtype: int32
print(df.iloc[3])
A 12
B 13
C 14
D 15
Name: 2017-11-22 00:00:00, dtype: int32
print(df.iloc[1:3])
A B C D
2017-11-20 4 5 6 7
2017-11-21 8 9 10 11
print(df.iloc[1:3,1:3])
B C
2017-11-20 5 6
2017-11-21 9 10
print(df.iloc[[1,5,3],1:3])
B C
2017-11-20 5 6
2017-11-24 21 22
2017-11-22 13 14
print(df.ix[:2,[‘A’,’C’]])
A C
2017-11-19 0 2
2017-11-20 4 6
print(df[df.A>8])
A B C D
2017-11-22 12 13 14 15
2017-11-23 16 17 18 19
2017-11-24 20 21 22 23
print(df.A>8)
2017-11-19 False
2017-11-20 False
2017-11-21 False
2017-11-22 True
2017-11-23 True
2017-11-24 True
Freq: D, Name: A, dtype: bool
df.iloc[2,2]=50
print(df)
A B C D
2017-11-19 0 1 2 3
2017-11-20 4 5 6 7
2017-11-21 8 9 50 11
2017-11-22 12 13 14 15
2017-11-23 16 17 18 19
2017-11-24 20 21 22 23
df.loc[‘20171119’,’B’]=56
print(df)
A B C D
2017-11-19 0 56 2 3
2017-11-20 4 5 6 7
2017-11-21 8 9 50 11
2017-11-22 12 13 14 15
2017-11-23 16 17 18 19
2017-11-24 20 21 22 23
df[df.A>4]=0
print(df)
A B C D
2017-11-19 0 56 2 3
2017-11-20 4 5 6 7
2017-11-21 0 0 0 0
2017-11-22 0 0 0 0
2017-11-23 0 0 0 0
2017-11-24 0 0 0 0
df=pandas.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=[‘A’,’B’,’C’,’D’])
print(df)
A B C D
2017-11-19 0 1 2 3
2017-11-20 4 5 6 7
2017-11-21 8 9 10 11
2017-11-22 12 13 14 15
2017-11-23 16 17 18 19
2017-11-24 20 21 22 23
df.A[df.A>4]=0
print(df)
A B C D
2017-11-19 0 1 2 3
2017-11-20 4 5 6 7
2017-11-21 0 9 10 11
2017-11-22 0 13 14 15
2017-11-23 0 17 18 19
2017-11-24 0 21 22 23
df[‘E’]=pandas.Series([1,2,3,4,5,6],index=pandas.date_range(‘20171119’,periods=6))
print(df)
A B C D E
2017-11-19 0 1 2 3 1
2017-11-20 4 5 6 7 2
2017-11-21 0 9 10 11 3
2017-11-22 0 13 14 15 4
2017-11-23 0 17 18 19 5
2017-11-24 0 21 22 23 6
“`