Pandas: Adding new column to dataframe which is a copy of the index column

后端 未结 2 493
花落未央
花落未央 2020-11-22 05:41

I have a dataframe which I want to plot with matplotlib, but the index column is the time and I cannot plot it.

This is the dataframe (df3):

but whe

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 06:39

    You can directly access in the index and get it plotted, following is an example:

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
    
    #Get index in horizontal axis
    plt.plot(df.index, df[0])
    plt.show()
    

     #Get index in vertiacal axis
     plt.plot(df[0], df.index)
     plt.show()
    

提交回复
热议问题