Coder1987
Coder1987

Reputation: 58

Plot a dataframe based on index values only

I have a simple question. I have a dataframe with multiple columns for date and 2 index rows like this:

enter image description here

I wish to plot this given dataframe as a lineplot but based on indexes. What I mean by this is, I want a plot with 2 lines: Ptf and bmk representing the values in respective indices. Any help is appreciated.

Upvotes: 0

Views: 260

Answers (1)

gioarma
gioarma

Reputation: 458

With a DataFrame like this:

data = pd.DataFrame(data = {'2021-01-01': [0,1],
                            '2021-01-02': [2.3, 2.4],
                            '2021-01-03': [3.1, 4.2]},
                  index=['ptf', 'bmk'])
data.columns = pd.to_datetime(data.columns)
print (data)

     2021-01-01  2021-01-02  2021-01-03
ptf           0         2.3         3.1
bmk           1         2.4         4.2

You can access the transposed data with the .T property and then use the .plot() command. The plot method plots each column as a function of the index, so by transposing you get exactly what you are asking:

data.T.plot()

enter image description here

Upvotes: 1

Related Questions