user3600497
user3600497

Reputation: 1661

How can I evenly space date data from a data frame?

I have a dataframe of measurements from an experiment.

enter image description here

I can easily plot the data from the data frame using pandas. Here is the result.enter image description here

The dates are evenly spaced on the axis, but in reality, they are not evenly spaced. How can I get an accurate representation of the time between measurements?

Here is my code for plotting the data frame:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

normal_df= pd.DataFrame(normal, columns = cols, index = rows[2::])
print normal_df

#Write the data frame to an xlsx file

normal_df.to_excel(csv_file[0:-4] + '_Normalized_Survival.xlsx')

avg = normal_df.mean()

errors = normal_df.sem()

avg.plot(marker = 'v',yerr = errors)
plt.title('Mean Survival with Standard Error',fontsize = 20)
plt.xticks(fontsize = 12,rotation = 45)
plt.yticks(fontsize = 12)
plt.xlabel('Time',fontsize = 18)
plt.ylabel('% Survival',fontsize = 18)
plt.xlim([0,6.1])
plt.legend(['Survival'])
plt.show()

Upvotes: 1

Views: 686

Answers (1)

qmorgan
qmorgan

Reputation: 4894

Here's one option you can try, by performing string operations to extract the integer Day and setting the index to be the resultant values

In [10]: cpy = [100, 89, 84, 73, 65, 6, 0]

In [11]: days = ['Day 1','Day 2','Day 3','Day 6','Day 9','Day 14','Day 16']

In [12]: df = pd.DataFrame({'day':days,'val':cpy})

In [13]: df['dayint'] = df.day.apply(lambda x : int(x.split(' ')[-1]))

In [14]: df.set_index(df.dayint, inplace=True)

In [15]: df.val.plot()

In [16]: plt.show()

Example Plot

Upvotes: 2

Related Questions