0

enter image description here

I want to draw the close price (y-axis) and date (x-axis) with python, but the error shows that I need to convert date from string to float.

Here is coding:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
import datetime

from pandas import DataFrame, Series

df = pd.read_csv('C:/Users/Vicky/Desktop/pythontest/T1706dailyrecord.csv')

df.columns = [1,2,3,4,5]
print(df)

plt.plot(df[1], df[3])
2
  • what is df1, can show your code, Commented May 25, 2017 at 5:44
  • Put your coding part in Code Sample format, to make it more clear Commented May 25, 2017 at 5:56

1 Answer 1

1

I think you need parameter parse_dates for convert column to datetime in read_csv:

df = pd.read_csv('C:/Users/Vicky/Desktop/pythontest/T1706dailyrecord.csv', parse_dates=[0])

Or:

df=pd.read_csv('C:/Users/Vicky/Desktop/pythontest/T1706dailyrecord.csv',parse_dates=['Date'])

Also df.columns = [1,2,3,4,5] is not necessary, for select use: df['Date'] and df['Close']:

plt.plot(df['Date'], df['Close'])

Also is possible use DataFrame.plot:

df.plot(x='Date', y='Close')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.