0

I am graphing data that is stored in a csv. I pull pull 2 columns of data into a dataframe then convert to series and graph with 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()

And this works totally fine. I would like to do the same and graph multiple rows but when I try to convert the df to series I am getting an error:

#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']

Error:

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

What can I do to fix this?

7
  • 1
    Why did you use (...) instead of [...] on the line that is erroring out? Commented Dec 18, 2018 at 18:02
  • Im sorry I had it like [...] then I started playing with the code to try to get it to work, and that was the last thing I tried and ended up copying that over. I will fix that. Commented Dec 18, 2018 at 18:04
  • 3
    pandas is looking for a column ('Distinct_FLD', 'Not_On_MM', 'API_Call_Count', 'CACHE_Count'), use not_cost.set_index('Date')[['Distinct_FLD','Not_On_MM','API_Call_Count','CACHE_Count']] to pass multiple columns Commented Dec 18, 2018 at 18:15
  • 2
    seems like "close as typo"? Commented Dec 18, 2018 at 18:16
  • 2
    You have a typo, add [ and ] wrapping your list Commented Dec 18, 2018 at 18:17

1 Answer 1

1

It seems that you are trying to convert the columns of a DataFrame into multiple Series, indexed by the 'Date' column of your DataFrame.

Maybe you can try:

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'] 

.
.
.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.