简体   繁体   English

月、年,含值 Plot、Pandas 和 MatPlotLib

[英]Month, Year with Value Plot, Pandas and MatPlotLib

I am trying to plot a time graph with month and year combined for my x and values for y.我正在尝试 plot 一个时间图,其中我的 x 和 y 值结合了月份和年份。 Python is reading my excel data with decimal points so won't allow to convert to %m %Y. Python 正在读取我的带小数点的 excel 数据,因此不允许转换为 %m %Y。 Any ideas?有任何想法吗?

MY EXCEL DATA我的 EXCEL 数据

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

You could try converting to "int" before converting to "str" in this line:在此行中转换为“str”之前,您可以尝试转换为“int”:

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

This should ensure that what gets stored does not include decimal points.这应该确保存储的内容不包括小数点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM