请选择 进入手机版 | 继续访问电脑版
麦粉社区
>
帖子详情

python学习篇-pandas库(十三)

数据挖掘 发表于 2019-12-27 15:03
发表于 2019-12-27 15:03:11
DataFrame多重索引
1、根据多重索引创建DataFrame
  1. import pandas as pd
  2. import numpy as np
  3. df = pd.DataFrame(np.arange(12).reshape(6,2),index=[list('AAABBB'),list('123123')],columns=['col01','col02'])
  4. df
复制代码
812155e059f0b63132.png
2、多重索引设置列名称
  1. df.index.names = ['frist','second']
  2. df
复制代码
714035e059f7a916ca.png
3、多重索引分组求和
  1. df.groupby('frist').sum()
复制代码
918845e059fde90a6a.png
4、行列名称转换
  1. print(df)
  2. df.stack()
复制代码
766245e05a3bde131d.png
5、索引转换
  1. print(df)
  2. df.unstack()
复制代码
970555e05a4089cc9d.png
6、条件查找
创建数据集
  1. # 示例数据
  2. data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
  3.         'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
  4.         'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
  5.         'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}

  6. labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
  7. df = pd.DataFrame(data, index=labels)
  8. df
复制代码
815855e05a4ab3ed58.png
查找age大于3的全部信息
  1. df[df['age']>3]
复制代码
112425e05a55152ec8.png
7、根据行列索引切片
  1. df.iloc[2:4,1:3]
复制代码
844345e05a59df08ff.png
8、多重条件查询
  1. df[(df['animal']=='cat')&(df['age']<3)]
复制代码
120445e05a6591dc10.png
9、按关键字查询
  1. df[df['animal'].isin(['cat','dog'])]
复制代码
711615e05a6ce4ce9c.png
10、按标签及列名查询
  1. df.loc[df.index[[3,4,8]],['animal','age']]
复制代码
480955e05a73d31061.png
11、多条件排序
  1. # 按照 age 降序,visits 升序排列
  2. df.sort_values(by=['age','visits'],ascending=[False,True])
复制代码
830725e05a79b0a761.png
12、多值替换
  1. # 将 priority 列的 yes 值替换为 True,no 值替换为 False
  2. df['priority'].map({'yes':'True','no':'False'})
复制代码
193065e05a873829b2.png
13、分组求和
  1. df.groupby('animal').sum()
复制代码
300905e05a8b64be7b.png
14、使用列表拼接成多个DataFrame
  1. temp_df1 = pd.DataFrame(np.random.randn(5, 4))  # 生成由随机数组成的 DataFrame 1
  2. temp_df2 = pd.DataFrame(np.random.randn(5, 4))  # 生成由随机数组成的 DataFrame 2
  3. temp_df3 = pd.DataFrame(np.random.randn(5, 4))  # 生成由随机数组成的 DataFrame 3

  4. print(temp_df1)
  5. print(temp_df2)
  6. print(temp_df3)

  7. pieces = [temp_df1, temp_df2, temp_df3]
  8. pd.concat(pieces)
复制代码
451935e05a9143f69f.png
15、找出表中和最小的列
  1. df = pd.DataFrame(np.random.random(size=(5, 10)), columns=list('abcdefghij'))
  2. print(df.sum())
  3. df.sum().idxmin() # idxmax(), idxmin() 为 Series 函数返回最大最小值的索引值
复制代码
823265e05a9a31ff1a.png
16、DataFrame中每个元素减去每一行的平均值
  1. pd.DataFrame(np.random.random(size=(5,3)))
  2. print(df)
  3. df.sub(df.mean(axis=1),axis=0)
复制代码
149615e05aa717570a.png
17、DataFrame分组,并得到每一组中最大三个数之和
  1. df = pd.DataFrame({'A': list('aaabbcaabcccbbc'),
  2.                    'B': [12, 345, 3, 1, 45, 14, 4, 52, 54, 23, 235, 21, 57, 3, 87]})
  3. print(df)
  4. df.groupby('A')['B'].nlargest(3).sum(level=0)
复制代码
347995e05ab80e3546.png
高级模式
B Color Image Link Quote Code Smilies
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

0回帖数 0关注人数 2957浏览人数
最后回复于:2019-12-27 15:03
快速回复 返回顶部 返回列表