月,年与值图,熊猫和MatPlotLib



我正在尝试绘制一个时间图,其中月份和年份组合为我的x和y的值。Python正在读取带有小数点的excel数据,因此不允许转换为%m % y。什么好主意吗?

MY EXCEL DATA

How python reads my data
0     3.0-2015.0
1     5.0-2015.0
3     6.0-2017.0
...
68       nan-nan
69       nan-nan
70       nan-nan
71       nan-nan'

# Code
import plotly
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import pandas as pd
import math
# Set Directory
workbook1 = 'GAP Insurance - 1.xlsx'
workbook2 = 'GAP Insurance - 2.xlsx'
workbook3 = 'GAP Insurance - 3.xlsx'
df = pd.read_excel(workbook1, 'Sheet1',)
# Set x axis

df['Time'] = (df['Month']).astype(str)+ '-' + (df['Year']).astype(str)
df['Time'] = pd.to_datetime(df['Time'], format='%m-%Y').dt.strftime('%m-%Y')

可以尝试转换为" into "在转换为"str"这一行:

df['Time'] = (df['Month']).astype(str)+ '-' + (df['Year']).astype(str)

这将确保存储的内容不包含小数点。

最新更新