【python】pandas display选项

import pandas as pd

 

1、pd.set_option('expand_frame_repr', False)

True就是可以换行显示。设置成False的时候不允许换行

 

2、pd.set_option('display.max_rows', 10)

pd.set_option('display.max_columns', 10)

显示的最大行数和列数,如果超额就显示省略号,这个指的是多少个dataFrame的列。如果比较多又不允许换行,就会显得很乱。

 

3、pd.set_option('precision', 5)

显示小数点后的位数

 

4、pd.set_option('large_repr', A)  ??

 truncate表示截断,info表示查看信息,一般选truncate

 

5、pd.set_option('max_colwidth', 5)

列长度

 

6、pd.set_option('chop_threshold', 0.5)

绝对值小于0.5的显示0.0

 

7、pd.set_option('colheader_justify', 'left')

 显示居中还是左边,

 

8、pd.set_option('display.width', 200)

横向最多显示多少个字符, 一般80不适合横向的屏幕,平时多用200.




 

 pandas 再次复习基础 未完待续 

import pandas as pd
import numpy as np

loc[行标签:行标签,纵:yyy]
iloc[行索引:总索引,纵:yyy]
at[行标签,纵标签] 单元格
iat[行索引:xx,纵索引:xxx]

# 1 基础认识 header ,names
df1 = pd.read_csv(r"C:\Users\WY\Desktop\abc.csv", #注意打开的文件一定保持utf-8 的编码类型 否则不予打开
header=None,# 来禁止将第一行当做列名 指定文件无列名,默认列名失效 (第一行)
# df1 0 1 2
# 0 数字1 数字2 数字3
# 1 4.556325895 4.946748719 9.240498647
names=["我的1","我的2","我的3"],#自定义表头的内容
# 我的1 我的2 我的3
# 0 数字1 数字2 数字3
# 1 4.556325895 4.946748719 9.240498647

)
# print("df1不带参数的",df1) #
# df1 A B C
# 0 4.556326 4.946749 9.240499
# 1 6.798123 9.205499 6.495702
# 2 1.708867 1.949048 5.016719
# 3 7.687936 4.288530 0.447928


# 2 df.apply() 函数是df 的一个函数式接口,表示一次对df的列(A/B/C)进行迭代,

df2 = pd.read_csv(r"C:\Users\WY\Desktop\abc.csv")
df2_new = df2.apply(lambda col:(col-col.mean() *1.0)/col.std()) #每一列进行均值为0,方差为1的变换
# print(df2_new)
# 数字1 数字2 数字3
# 0 -0.102247 0.100417 1.865099
# 1 0.652462 1.435738 0.944303


# 3 如下

df3 = pd.read_excel(r"C:\Users\WY\Desktop\dongwu1.xlsx",sheet_name=1) #不知道为啥 还需要xlrd

# print(df3.head()) #默认前五项
# print("======")
# print(df3.tail()) #默认后五项
# print(df3)


# 求2015年06月的 基金代名称以东吴新开头的, ???map 的用法配合lamda
# print(df3[u"日期"].filter(lambda x:x))
# print(df3[u"日期"].map(lambda x:x.startswith("2015/7"))) # 错误的 错误的

# print(df3[df3[u"名称"].map(lambda x:x.startswith("东吴新"))][u"代码"])


# 4 基础练习创建对象
# 4-1 创建一个Series 对象
s = pd.Series([1,3,4,np.nan,6])
# print(s)

# 0 1.0
# 1 3.0
# 2 4.0
# 3 NaN #空的表示方法
# 4 6.0
# dtype: float64

# 4-2 可以通过传递一个带有datetime 索引和标签的numpy数组组件一个DataFrame
dates = pd.date_range("20181102",periods=3)
# print("dates:",dates)
# dates: DatetimeIndex(['2018-11-02', '2018-11-03', '2018-11-04'], dtype='datetime64[ns]', freq='D')

# 创建一个三行五列的,索引dates 变量的,行索引为dates 变量,列索引为列表的
df4 = pd.DataFrame(np.random.randn(3,5),index=dates,columns=list(("ABCDE")))
# print("df4:",df4)
# df4: A B C D E
# 2018-11-02 2.184946 -0.033344 -2.236584 -0.866299 -1.008643
# 2018-11-03 0.696904 1.792282 -0.665212 -1.563407 0.476134
# 2018-11-04 2.655982 1.043714 0.135831 -1.368920 -0.467744

# 也可以通过传递一个对象的字典创建一个DatarFrame,其中必须能够被转化成类似于series 形式的

df5 = pd.DataFrame({
"A":1,
"B":pd.Timestamp("20181102"),
"C":pd.Series(1,index=list(range(4))),
"D":np.array([3]*4,dtype="int32"),
"E":pd.Categorical(["test","train","test1","train1"]),
"F":'foo'
})
# print(df5)
# A B C D E F
# 0 1 2018-11-02 1 3 test foo
# 1 1 2018-11-02 1 3 train foo
# 2 1 2018-11-02 1 3 test1 foo
# 3 1 2018-11-02 1 3 train1 foo
# print("df5数据格式",df5.dtypes)
# df5数据格式 A int64
# B datetime64[ns]
# C int64
# D int32
# E category
# F object
# dtype: object

# 5 查看frame 中的索引,列名和数据

# print(df5.index) #索引
# Int64Index([0, 1, 2, 3], dtype='int64')

# print(df5.values)
# [[1 Timestamp('2018-11-02 00:00:00') 1 3 'test' 'foo']
# [1 Timestamp('2018-11-02 00:00:00') 1 3 'train' 'foo']
# [1 Timestamp('2018-11-02 00:00:00') 1 3 'test1' 'foo']
# [1 Timestamp('2018-11-02 00:00:00') 1 3 'train1' 'foo']]


# 6 利用describe 方法快速计算出数据的统计描述

# print(df5.describe())
# A C D
# count 4.0 4.0 4.0
# mean 1.0 1.0 3.0
# std 0.0 0.0 0.0
# min 1.0 1.0 3.0
# 25% 1.0 1.0 3.0
# 50% 1.0 1.0 3.0
# 75% 1.0 1.0 3.0
# max 1.0 1.0 3.0

# 7 对数据进行转置的操作就是横向索引变纵向索引
# print(df5.T)
# 0 ... 3
# A 1 ... 1
# B 2018-11-02 00:00:00 ... 2018-11-02 00:00:00
# C 1 ... 1
# D 3 ... 3
# E test ... train1
# F foo ... foo
#
# [6 rows x 4 columns]
# 7 排序相关
# 7-1 按照坐标抽进行排序,axis=0时,根据每行的datetime 索引进行排序(ascending=False)倒叙
# print(df5.sort_index(axis=0,ascending=False))
# A B C D E F
# 3 1 2018-11-02 1 3 train1 foo
# 2 1 2018-11-02 1 3 test1 foo
# 1 1 2018-11-02 1 3 train foo
# 0 1 2018-11-02 1 3 test foo

# 7-2 当axis=1时按照每列属性的名称对列的顺序进行排序,只是axis=1,ascending=False

# print(df5.sort_index(axis=1,ascending=False))
#
# F E D C B A
# 0 foo test 3 1 2018-11-02 1
# 1 foo train 3 1 2018-11-02 1
# 2 foo test1 3 1 2018-11-02 1
# 3 foo train1 3 1 2018-11-02 1

# print(df4.sort_values(by="C")) # 索引为C 的从小到大排序
# A B C D E
# 2018-11-03 -0.183651 -0.531373 -0.777123 -0.945526 -0.126268
# 2018-11-04 -0.268125 -0.059175 -0.711266 0.554662 -1.180428
# 2018-11-02 -0.335975 1.076329 0.122194 -2.365692 -1.006265


# 8 选择selection
# 获取数据,选择一列数据,生成一个Series,相当于df4.A

# print(df4["A"])
# print(df4.A)
# 2018-11-02 -0.286413
# 2018-11-03 -0.714055
# 2018-11-04 0.855449
# Freq: D, Name: A, dtype: float64

# 8-1 通过[] 进行选择,对行进行切片(取前两行数据)
# print(df4[0:2]) #注意索引顾头不顾尾
# A B C D E
# 2018-11-02 1.91919 0.277995 -0.131593 0.565316 0.400376
# 2018-11-03 1.12388 1.162995 -0.438591 -1.850703 1.497848

# 通过行名字索引进行选择

# print(df4["2018-11-02":"2018-11-04"])
# A B C D E
# 2018-11-02 1.006919 0.528077 -2.664482 1.044198 1.007990
# 2018-11-03 1.678051 1.031121 1.557632 0.788842 -0.596083
# 2018-11-04 0.512426 0.282755 1.552109 -0.711369 -0.704182

# 8-3 根据标签进行选择,利用标签获取截面数据(选择某一行后,筛选出列索引对应的数据)
# print(dates)
# DatetimeIndex(['2018-11-02', '2018-11-03', '2018-11-04'], dtype='datetime64[ns]', freq='D')
# print(dates[0]) #2018-11-02 00:00:0
# print(df4.loc[dates[0]])

# A -1.235993
# B 0.952721
# C -0.154217
# D -0.017963
# E 1.312850
# Name: 2018-11-02 00:00:00, dtype: float64

# 8-4利用标签获取多个属性的数据 (获取某几列的数据)
# print(df4.loc[:,["A","C"]])
# A C
# 2018-11-02 -0.420318 0.942901
# 2018-11-03 0.213130 -2.629690
# 2018-11-04 -0.897716 0.056602

# 8-4 获取标签的切片属性,起始点和终点都包括在内。 (选择标签行,然后选择列的范围)

# print(df4.loc["2018-11-01":"2018-11-03",["A","E"]])
# A E
# 2018-11-02 1.601100 0.982794
# 2018-11-03 -0.194842 0.190930

# 可以减少数据的维度和 8-4一样横向纵向 确定范围
# print(df4.loc["2018-11-03",["D"]])
# D -0.452131
# Name: 2018-11-03 00:00:00, dtype: float64

# 8-5 可以获取单个纯量值
# print(dates[2]) #2018-11-04 00:00:00
# print(df4.loc[dates[2],"D"]) #f方式1 -0.7132192796242404
# print(df4.at[dates[2],"D"]) #方式二

# 9 根据位置进行选择
# 利用坐标位置获取数据
# print(df4)
# print(df4.iloc[2]) # 根据行索引的2 选择第三行的数据,取得结果方式有点不一样纵向排列
# A 0.116833
# B 0.024451
# C -0.032890
# D -0.022540
# E -1.741453
# Name: 2018-11-04 00:00:00, dtype: float64

# 9-2 返回切片数据,和numpy/python 中的操作相似通过索引 行索引和列索引过滤取值
# print(df4)
# print(df4.iloc[0:2,3:5])
# A B C D E
# 2018-11-02 0.518129 -1.008438 -0.270113 -0.722813 -0.742989
# 2018-11-03 -1.921633 0.435408 0.106355 -0.957731 -1.141763
# 2018-11-04 -0.335671 -0.232652 -0.324051 1.573310 -0.459856
# D E
# 2018-11-02 -0.722813 -0.742989
# 2018-11-03 -0.957731 -1.141763

# 9-3对行进行切片 (行切片,列冒号,代表所有的)
# print(df4.iloc[1:3,:])
# A B C D E
# 2018-11-03 -1.888404 1.367640 0.833580 0.039805 0.071017
# 2018-11-04 -0.001811 0.682274 1.290628 -1.127835 -0.111181

# 9-4 对列进行切片
# print(df4.iloc[:,3:5])
# D E
# 2018-11-02 -2.093455 -0.707597
# 2018-11-03 -2.153583 0.906992
# 2018-11-04 -2.630041 0.728587


# 9-5 获取一个值
# print(df4.iloc[2,4]) #0.017823521018910517
# print(df4.iat[2,4]) #快速获取一个值 与上面的等价

# 10 布尔索引
# 10-1利用一列的数据选择一部分数据

# print(df4[df4.A>0]) #选判断 df4.A>0 有几个,然后根据有的那几个,在确定整行的数据
# A B C D E
# 2018-11-03 0.463002 -0.153413 -0.728819 -1.564213 -1.614742
# 2018-11-04 0.491805 0.739841 0.554370 0.850707 -0.660782

# 11 类似于where 操作用于获取数据 (df4 是整个DataFrame 数据框,df4>0 代表除行列索引的每一个数据,不符合的为NaN)

# print(df4[df4>0])
# A B C D E
# 2018-11-02 0.036594 0.333475 0.165353 0.869498 NaN
# 2018-11-03 NaN 0.300629 1.256110 NaN 0.663619
# 2018-11-04 NaN 1.227524 NaN NaN 1.171186

# 12 利用isin() 方法过滤数据

df_4 = df4.copy()
# print(df_4["E"])
# 2018-11-02 0.256932
# 2018-11-03 0.414216
# 2018-11-04 -0.207306
# Freq: D, Name: E, dtype: float64

# df_4["E"]=["ONE","TWO","THREE"] #通过标签名字 更改内容
# print(df_4)
# A B C D E
# 2018-11-02 1.409377 -2.332408 -0.025918 -0.521506 ONE
# 2018-11-03 -0.504533 0.577214 -1.941061 -0.908369 TWO
# 2018-11-04 -0.376786 -1.439106 1.096957 -0.733258 THREE

# 关于isin() 的用法 就是过滤数据
df_4["E"]=["ONE","TWO","THREE"] #通过标签名字 更改内容
# print(df_4[df_4["E"].isin(["TWO"])])
# A B C D E
# 2018-11-03 -0.980729 -0.138065 -0.99163 -0.711267 TWO

# 13 赋值 创建新的一列,数据根据索引自动对齐。(一定注意:例s1 的行索引必须和被赋值的索引是一样,否则是不成功的)
s1 = pd.Series([1,2,3],index=pd.date_range("20181102",periods=3))
# print(s1)
# 2018-11-02 1
# 2018-11-03 2
# 2018-11-04 3
# Freq: D, dtype: int64

# print(df4["E"])
# 2018-11-02 1.162631
# 2018-11-03 -2.058542
# 2018-11-04 -0.747192
# Freq: D, Name: E, dtype: float64

df_4["E"] = s1 #方式1 变量赋值 S1 的行索引名称必须和df_4 的索引名称是一致才能完成赋值

# 13-1 利用标签进行赋值 具体的位置
df_4.at[dates[1],"A"]=1111 # 利用标签进行赋值,常量赋值
df_4.iat[1,2] = 22222 #利用位置坐标进行赋值
df_4.loc[:,"D"] = np.array([5]*len(df_4)) # 注解:np.array()
# print(df_4)
# A B C D E
# 2018-11-02 -1.622113 -0.221579 0.625811 5 1
# 2018-11-03 1111.000000 0.541201 22222.000000 5 2
# 2018-11-04 1.210669 -1.364742 1.320921 5 3

# 注解一下
a = np.array([4]*5)
b = np.array(4*5)
# print(a,b) #[4 4 4 4 4] 20


# 二 数据缺失

# pandas 主要利用np.nan 表示缺失数据,默认是缺失数据不参与计算。
# reindex 方法允许我们改变,添加,删除某个属性上的索引,这一操作会返回数据的一个拷贝。

df6 = df4.reindex(index=dates[0:4],columns=list(df4.columns)+["G"])
print(df6)
[[]]