【Pandas】莫烦教学笔记


axis代表轴向 0代表水平 1代表垂直 丢弃数据的时候当axis=0表示按水平方向丢掉数据

3.1 Pandas基础介绍

import pandas as pd
import numpy as np

s = pd.Series([1, 3, 6, np.nan, 44, 1])  # 序列
print(s)
# 输出
# 0     1.0
# 1     3.0
# 2     6.0
# 3     NaN
# 4    44.0
# 5     1.0
# dtype: float64

dates = pd.date_range('20160101', periods=6)  # 描述行的索引
print(dates)
# 输出
# DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04',
#                '2016-01-05', '2016-01-06'],
#               dtype='datetime64[ns]', freq='D')

df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['a', 'b', 'c', 'd'])
# DataFrame 一个大的matrix 类似于二维的numpy
print(df)

df1 = pd.DataFrame(np.arange(12).reshape((3, 4)))  # np.arange(12) 生成0-11的序列
print(df1)

df2 = pd.DataFrame({
    
    
    'A': 1.,
    'B': pd.Timestamp('20130102'),  # 以日期格式输出
    'C': pd.Series(1, index=list(range(4)), dtype='float32'),
    'D': np.array([3] * 4, dtype='int32'),
    'E': pd.Categorical(["test", "train", "tes", "tra"]),
    'F': 'foo'
})
print(df2)
print(df2.dtypes)  # 所有列的类型
print(df2.index)  # 所有行的序号以及类型
print(df2.columns)  # 所有列的名字以及类型
print(df2.values)  # 所有的值
print(df2.describe())  # 描述数据
print(df2.T)  # 转置矩阵
#                    对列名称进行排序  以倒的序列排序
print(df2.sort_index(axis=1, ascending=False))
print(df2.sort_index(axis=0, ascending=False))
print(df2.sort_values(by='E'))  # 对单行的值进行排序

3.2 Pandas 选择数据 标签/数字/Boolean筛洗

用到的数据:

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'])

print(df)

输出:

             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
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'])

print(df['A'], df.A)  # 选择第A行
print(df[0:3], df['20130102':'20130104'])  # 输出选取的行

# select by label:loc 标签筛选
print(df.loc['20130102'])  # 以标签的名字选择
print(df.loc[:, ['A', 'B']])  # 打印所有行的A、B两列
print(df.loc['20130102', ['A', 'B']])  # 选择特定标签的列

# select by position:iloc 数字筛选
print(df.iloc[3])
print(df.iloc[3, 1])
print(df.iloc[3:5, 1:3])
print(df.iloc[[1, 3, 5], 1:3])

# mixed selection:ix 标签+数字筛选 但是anaconda不支持
# print(df.ix[:3, ['A', 'C']])

# Boolean indexing 是或否 显示所有
print(df)
print(df[df.A > 8])  # 只对比A,但是还是显示所有的数据

3.3 Pandas 设置值

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'] = 222
# df[df.A > 4] = 0  # 改所有列
df.B[df.A > 4] = 0  # 只改B列
df['F'] = np.nan  # 加一列空的column
df['E'] = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range('20130101', periods=6))  # 加非空的column
print(df)

3.4 Pandas 处理丢失数据

猜你喜欢

转载自blog.csdn.net/weixin_43232564/article/details/108072117