读取数据透视表多列形态数据作图

示例文件

import pandas as pd
import numpy as np
import datetime
today=str(datetime.date.today())

filepath='/Users/kangyongqing/Documents/kangyq/202404/NPS评分/'
file1='05NPS信息匹配分析2024-04-22.xlsx'

#从第三行开始读取列名,第一列作为索引
df1=pd.read_excel(filepath+file1,sheet_name='班型&年龄(季度)',header=0,index_col=0)
#缺失值填充,fillna(method='ffill'使用前一个值填充缺失值,fillna(method='bfill')使用后一个值填充缺失值
#新版本中直接使用ffill(),bfill(),老方法将报错
df1.loc['班型']=df1.loc['班型'].ffill()
df1.columns=df1.loc['班型',:]+df1.loc['年龄',:]
df1.index.name='季度'
df1.drop(index=['Qut','班型','年龄'],inplace=True)
print(df1.columns,df1.index)
print(df1.head(5))
print(df1.shape)

df2=df1.iloc[:,6:]
x=df2.index
y=df2.values

df2=df1.iloc[1:,7:]
print(df2)

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(10,6))
sns.lineplot(df2)
plt.title('季度NPS班型&年龄评分')
plt.savefig(filepath+'08季度NPS班型&年龄评分趋势.png')

#从第三行开始读取列名,第一列作为索引
df3=pd.read_excel(filepath+file1,sheet_name='班型&年龄(月)',header=0,index_col=0)
#缺失值填充,fillna(method='ffill'使用前一个值填充缺失值,fillna(method='bfill')使用后一个值填充缺失值
#新版本中直接使用ffill(),bfill(),老方法将报错
df3.loc['班型']=df3.loc['班型'].ffill()
df3.columns=df3.loc['班型',:]+df3.loc['年龄',:]
df3.index.name='月度'
df3.drop(index=['Mut','班型','年龄'],inplace=True)
print(df3.columns,df3.index)
print(df3.head(5))
print(df3.shape)

df4=df3.iloc[1:,7:]
print(df4)

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(10,6))
sns.lineplot(df4)
#控制X坐标轴字体角度
plt.xticks(rotation=30)
plt.title('月度NPS班型&年龄评分')
plt.savefig(filepath+'09月度NPS班型&年龄评分趋势.png')

作图结果:

使用到的常用处理方法:

  1. #从第*行开始读取列名,第一列作为索引
    df1=pd.read_excel(filepath+file1,sheet_name='班型&年龄(季度)',header=0,index_col=0)
  2. #缺失值填充,fillna(method='ffill'使用前一个值填充缺失值,fillna(method='bfill')使用后一个值填充缺失值
    #新版本中直接使用ffill(),bfill(),老方法将报错
    df1.loc['班型']=df1.loc['班型'].ffill()
    df1.columns=df1.loc['班型',:]+df1.loc['年龄',:]
  3. 重命名索引,并删除多余的行数据
    df1.index.name='季度'
    df1.drop(index=['Qut','班型','年龄'],inplace=True)
  4. 横轴标签较多是,调整字体角度,使完全显示
    import seaborn as sns
    import matplotlib.pyplot as plt
    plt.figure(figsize=(10,6))
    sns.lineplot(df4)
    #控制X坐标轴字体角度
    plt.xticks(rotation=30)
    plt.title('月度NPS班型&年龄评分')

相关推荐

  1. Oracle BIEE 示例(一)数据透视

    2024-04-22 17:14:03       52 阅读
  2. Iceberg: 读取Parquet数据

    2024-04-22 17:14:03       72 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-22 17:14:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-22 17:14:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-22 17:14:03       82 阅读
  4. Python语言-面向对象

    2024-04-22 17:14:03       91 阅读

热门阅读

  1. Flutter本地化存储介绍与使用

    2024-04-22 17:14:03       35 阅读
  2. Android Jetpack学习系列——WorkManager

    2024-04-22 17:14:03       34 阅读
  3. QML学习之加载gif

    2024-04-22 17:14:03       27 阅读
  4. QT6之qDeleteAll

    2024-04-22 17:14:03       31 阅读
  5. sizeof和strlen,len的区别

    2024-04-22 17:14:03       32 阅读
  6. 华为笔试面试题

    2024-04-22 17:14:03       24 阅读
  7. 头歌平台云计算实验

    2024-04-22 17:14:03       30 阅读
  8. vue中 export default 与 export 写法的区别

    2024-04-22 17:14:03       33 阅读
  9. 日本语自然语言处理中的分词库 - GiNZA

    2024-04-22 17:14:03       32 阅读