为Matplotlib创建系列时出现Python键错误
我正在绘制存储在csv中的数据。我将两列数据拉入一个数据框,然后用matplotlib转换为series和graph为Matplotlib创建系列时出现Python键错误,python,pandas,matplotlib,Python,Pandas,Matplotlib,我正在绘制存储在csv中的数据。我将两列数据拉入一个数据框,然后用matplotlib转换为series和graph 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
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转换为series时,我得到一个错误:
#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')
如何解决此问题?您似乎正在尝试将数据帧的列转换为多个系列,并以数据帧的“日期”列为索引 也许你可以试试:
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']
.
.
.
为什么在出错的线路上使用
(…)
而不是[…]
?很抱歉,我像[…]一样使用了它,然后我开始玩代码,试图让它工作,这是我最后一次尝试,最后复制了一遍。我会解决这个问题。熊猫正在寻找一个列('Distinct_FLD'、'Not_On_MM'、'API_Call_Count'、'CACHE_Count'),使用Not cost。设置索引('Date')[['Distinct_FLD'、'Not_On_MM'、'API_Call_Count'、'CACHE_Count']]来传递多个列,如“close as typo”?如果您有输入错误,请添加[
和]
包装您的列表