英文:
How to turn a string column of quarterly data in pandas into sompething I can plot
问题
我有一个看起来像这样的数据框:
错误:
Quarter London UK NaN
6 1992年3月至5月 0.12305 0.098332 NaN
7 1992年4月至6月 0.123895 0.097854 NaN
8 1992年5月至7月 0.124076 0.098878 NaN
9 1992年6月至8月 0.127796 0.099365 NaN
10 1992年7月至9月 0.126064 0.099371 NaN
我尝试使用Quarter列上的PeriodIndex以便绘制数据,但它一直给我报错。我可以尝试下一步做什么?
我尝试使用的代码是:
quarter_column = df.Quarter
# 创建PeriodIndex
periods = pd.PeriodIndex(quarter_column, freq='Q-MAR')
pd.DataFrame(df, index=periods)
报错信息如下:
DateParseError: 未知的日期时间字符串格式,无法解析:1992年3月至5月
英文:
I have a dataframe that looks like this:
oops:
Quarter London UK NaN
6 Mar-May 1992 0.12305 0.098332 NaN
7 Apr-Jun 1992 0.123895 0.097854 NaN
8 May-Jul 1992 0.124076 0.098878 NaN
9 Jun-Aug 1992 0.127796 0.099365 NaN
10 Jul-Sep 1992 0.126064 0.099371 NaN
I've tried to use the PeriodIndex on quarter so I can plot the data but it just keeps giving me errors. What can I try next?
The code that I was trying to use:
quarter_column = df.Quarter
# create PeriodIndex
periods = pd.PeriodIndex(quarter_column, freq='Q-Mar')
pd.DataFrame(df, index=periods)
The error was:
DateParseError: Unknown datetime string format, unable to parse: MAR-MAY 1992
答案1
得分: 1
显然PeriodIndex
没有接受该格式字符串。您的季度也有重叠,这使得定义PeriodIndex
变得具有挑战性。
不过,您是否需要将其转换为PeriodIndex
?如果您的数据已经按顺序排序并以连续方式索引(6、7、8、9...),您可以使用以下代码:
# 将数据绘制在数据框的索引上。
# 例如,伦敦系列实际上绘制为:
# (6, 0.12305), (7, 0.123895), (8, 0.124076), ...
ax = df[["London", "UK"]].plot(marker="o")
# 将x轴标记与索引相同,即:6, 7, 8...,而不是6, 6.5, 7, 7.5...
ax.xaxis.set_ticks(df.index)
# 标记刻度
ax.xaxis.set_ticklabels(df["Quarter"])
结果:
英文:
Obviously PeriodIndex
didn't accept that format string. Your quarters also overlap which makes it challenging to define a PeriodIndex
.
However, do you need to convert it to PeriodIndex
? If your data is already sorted and indexed sequentially (6, 7, 8, 9...), you can use the code below:
# Plot the data against the dataframe's index.
# For example, the London series is actually plotted as:
# (6, 0.12305), (7, 0.123895), (8, 0.124076), ...
ax = df[["London", "UK"]].plot(marker="o")
# Tick the x-axis the same as the index, i.e: 6, 7, 8..., not 6, 6.5, 7, 7.5...
ax.xaxis.set_ticks(df.index)
# Label the ticks
ax.xaxis.set_ticklabels(df["Quarter"])
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论