为Matplotlib创建系列时出现Python KeyError

时间:2018-12-18 18:00:50

标签: python pandas matplotlib

我正在对存储在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')

该如何解决?

1 个答案:

答案 0 :(得分:1)

似乎您正在尝试将DataFrame的列转换为多个Series,并按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'] 

.
.
.