user13770492
user13770492

Reputation: 39

Pandas - How to plot relative values

I am trying to plot time on x axis.I want the relative time to be plotted instead of time stamp.

    data = {'Person':['A', 'A', 'A', 'A','A','A','A'], 'timestamp':[1591828693236, 1591828693237, 1591828693266, 1591828693271, 1591828693294, 1591828693297,1591828693307] ,'wage':[10,15,17,30,60,15,55]}
    df = pd.DataFrame(data)  

    df = df.groupby(['Person'])['wage'].cumsum()
    df.plot(x='timestamp',y=['wage']) 

This plots the original timestamp with the cumulative wage.How do i get the relative timestamps in the x axis?

Upvotes: 1

Views: 459

Answers (1)

wwii
wwii

Reputation: 23753

Subtract the first value of the Series from the Series and set that as the DataFrame's index.

data = {'Person':['A', 'A', 'A', 'A','A','A','A'], 'timestamp':[1591828693236, 1591828693237, 1591828693266, 1591828693271, 1591828693294, 1591828693297,1591828693307] ,'wage':[10,15,17,30,60,15,55]}
df = pd.DataFrame(data)  
df.index = df['timestamp'] - df.loc[0,'timestamp']
gb = df.groupby(['Person'])['wage'].cumsum()
gb.plot(x='timestamp',y=['wage'])

Upvotes: 4

Related Questions