import pandas as pd
import numpy as np
# Series 相当于一个有序字典 表格形式,添加索引
s1 = pd.Series([4, 7, -5, 3])
print(s1)
print(s1.values)
print(s1.index)
s2 = pd.Series([1, 3, -5, -4], index=["a", "b", "c", "d"])
print(s2)
# 根据索引取值
print(s2["b"])
# 判断索引是否存在
print("b" in s2)
# DataFrame 相当于一个表格
data = {"year": [2014, 2015, 2016],
"income": [20000, 12000, 15000],
"pay": [6500, 6000, 7000]
}
print(data)
df1 = pd.DataFrame(data)
print(df1)
df2 = pd.DataFrame(np.arange(12).reshape(3, 4))
print(df2)
df3 = pd.DataFrame(np.arange(12).reshape((3, 4)), index=["a", "d", "b"], columns=["I", "II", "IV", "III"])
print(df3)
# df1的列
print(df1.columns)
# df1的行
print(df1.index)
print(df1.values)
# df1的总结
print(df1.describe())
print(df1.T)
print("=" * 60)
print(df3)
# 按行索引排序
print(df3.sort_index(axis=0))
# 按列索引排序
print(df3.sort_index(axis=1))
# 按值排序
print(df3.sort_values(by='I'))
import pandas as pd
import numpy as np
datas =pd.date_range("20191121", periods=6)
df1 = pd.DataFrame(np.arange(24).reshape(6, 4), index=datas, columns=["A", "B", "C", "D"])
print(df1)
# 将其中一列 获取为series
print(df1["A"])
print(df1.A)
# 切片
print(df1[0:2])
print(df1["2019-11-24":"2019-11-26"])
# 通过行便签 选择列
print(df1.loc["2019-11-24"])
print(df1.loc["2019-11-24", ["A", "C"]])
# 选择ac 两列的数据
print(df1.loc[:, ["A", "C"]])
# 第二行
print(df1.iloc[2])
# 某几行,某几列
print(df1.iloc[1:3, 2:4])
print(df1.iloc[[1, 2, 4], [1, 3]])
# 混合标签位置选择
# print(df1.ix[2:4, ["A", "C"]])
# print(df1.ix["2019-11-24":"2019-11-26", 2:4])
# 判断
print(df1.A > 6)
# 通过true 或false进行筛选
print(df1[df1.A > 6])