如何从数据框中均匀分隔日期数据?

时间:2015-07-16 17:41:36

标签: python date pandas matplotlib plot

我有一个实验测量数据框。

enter image description here

我可以使用pandas轻松地绘制数据框中的数据。结果如下:enter image description here

日期在轴上均匀分布,但实际上,它们的间距不均匀。如何准确表示测量之间的时间?

以下是我绘制数据框的代码:

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()

1 个答案:

答案 0 :(得分:2)

这是您可以尝试的一个选项,通过执行字符串操作来提取整数Day并将索引设置为结果值

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