Oscar Batalla
Oscar Batalla

Reputation: 25

Is there a way to plot two columns of a data frame in the same axis?

I have a dataframe which consists of 3 colums: 'Date', 'Time' and 'Data' I want to plot 'Date' and 'Time' and the X axis and 'Data' on the Y axis. So far, since I used pandas to import my data set, I tried with:

df.plot(x= "Date", y="Data")

but I think it only allows one argument in 'x='

I also wonder if in this case, is better to define 'Date' and 'Time' as a single column

Thank you for your support!

Upvotes: 1

Views: 425

Answers (1)

Matteo Zanoni
Matteo Zanoni

Reputation: 4142

You can create a new column called "DateTime" that combines "Date" and "Time".

Then plot it on the x axis and "Data" on the y axis:

import pandas as pd

df = ...

df["DateTime"] = pd.to_datetime(df['Date'].astype(str) + df['Time'].astype(str), format="%m-%d-%Y%H:%M:%S")
df.plot(x="DateTime", y="Data")

Upvotes: 1

Related Questions