为Matplotlib创建系列时出现Python键错误



我正在绘制存储在csv中的数据。我将2列数据拉入一个数据帧,然后使用matplotlib转换为序列和图形。

from pandas import Series
from matplotlib import pyplot
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('Proxy/Proxy_Analytics/API_Statistics.csv')
df
Date        Distinct_FLD    Not_On_MM   API_Call_Count  Cost     CACHE_Count
0   2018-11-12  35711           18468       18468           8.31060  35711
1   2018-11-13  36118           18741       11004           4.95180  46715
2   2018-11-14  34073           17629       8668            3.90060  55383
3   2018-11-15  34126           17522       7817            3.51765  63200
#Cost
cost_df = df[['Date','Cost']]
cost_series = cost_df.set_index('Date')['Cost']
plt.style.use('dark_background')
plt.title('Domain Rank API Cost Over Time')
plt.ylabel('Cost in Dollars')
cost_series.plot(c = 'red')
plt.show()

这完全可以。我想做同样的事情并绘制多行图,但当我试图将df转换为序列时,我遇到了一个错误:

#Not Cost
not_cost = df[['Date','Distinct_FLD','Not_On_MM','API_Call_Count','CACHE_Count']]
not_cost_series = not_cost.set_index('Date')['Distinct_FLD','Not_On_MM','API_Call_Count','CACHE_Count']

错误:

KeyError: ('Distinct_FLD', 'Not_On_MM', 'API_Call_Count', 'CACHE_Count')

我能做些什么来解决这个问题?

您似乎正试图将DataFrame的列转换为多个系列,并通过DataFrame的"Date"列进行索引。

也许你可以试试:

not_cost = df[['Date','Distinct_FLD','Not_On_MM','API_Call_Count','CACHE_Count']]
not_cost_series = not_cost.set_index('Date')
Distinct_FLD    = not_cost_series['Distinct_FLD']
Not_On_MM       = not_cost_series['Not_On_MM'] 
.
.
.

最新更新