我根据您的反馈修改了我的答案,以更准确地重现问题。
有了这个:
df_topex = pd.read_csv('datasets/TOPEX.dat',
sep='\s+', #multiple spaces as separator
index_col=0, #convert first column to index
names=["Time", "Anomaly"], #naming the headers
)
你有这样的东西,其中“时间”列是索引:
Time Anomaly
--------- ---------
1992.9595 2.0000
1992.9866 3.0000
1993.0138 4.0000
1993.0409 5.0000
1993.0681 6.0000
1993.0952 7.0000
要绘制它,我们可以按照您说的执行以下操作,但仅供参考,此方法存在问题(https://github.com/pandas-dev/pandas/issues/16529,但目前不是什么大问题):
df_topex.reset_index(inplace=True)
tabulate_df(df_topex)
这可能更安全:
df_topex = df_topex.reset_index()
无论如何,我们已经准备好在绘图中使用“时间”作为列(我指出“时间”在我看来没有时间格式):
Time Anomaly
------ --------- ---------
0 1992.9595 2.0000
1 1992.9866 3.0000
2 1993.0138 4.0000
3 1993.0409 5.0000
4 1993.0681 6.0000
5 1993.0952 7.0000
绘制它:
df_topex.plot(kind='scatter', x='Time', y='Anomaly', color='red')
那么让我们按照你的最后一个问题来思考:嗯......我们已经得到了情节,但是现在我们不能利用使用“时间”作为索引的优势,不是吗?
索引在过滤数百万行时具有显着的性能影响。也许您有兴趣使用“时间”列作为索引,因为您已经或预见到高容量。可以绘制数百万个点(例如数据着色),但不是很常见。在绘制之前过滤任何 DataFrame 是很常见的,在这一点上,对要过滤的列进行索引真的很有帮助,之后通常会出现绘图。
所以我们可以分阶段使用不同的 DataFrame,或者在 csv 导入操作之后完全执行以下操作,即保持索引与它一起使用并随时在 Time2 列上绘图:
df_topex['Time2'] = df_topex.index
所以我们将“时间”作为索引:
Time Anomaly Time2
--------- --------- ---------
1992.9595 2.0000 1992.9595
1992.9866 3.0000 1992.9866
1993.0138 4.0000 1993.0138
1993.0409 5.0000 1993.0409
1993.0681 6.0000 1993.0681
1993.0952 7.0000 1993.0952
如何利用索引?
很好的帖子,其中测量了过滤索引的性能:What is the performance impact of non-unique indexes in pandas?
简而言之,您对拥有唯一索引或至少已排序感兴趣。
# Performance preference in index type to filtering tasks:
# 1) unique
# 2) if not unique, at least sorted (monotonic increase o decrease)
# 3) Worst combination: non-unique and unsorted.
# Let's check:
print ("Is unique?", df_topex.index.is_unique)
print ("Is is_monotonic increasing?", df_topex.index.is_monotonic_increasing)
print ("Is is_monotonic decreasing?", df_topex.index.is_monotonic_decreasing)
来自样本数据:
Is unique? True
Is is_monotonic increasing? True
Is is_monotonic decreasing? False
如果未排序,您可以通过以下方式执行排序任务:
df_topex = df_topex.sort_index()
# Ready to go on filtering...
希望对你有帮助。