BIG EDIT: This error will not throw if I remove index_col = 1 from the read_csv function. Leaving this question up because I am curious why this happens.
I have the following CSV:
Agency Division Expenditures ($000,000)
NYPD OPERATIONS 3331
NYPD EXECUTIVE MANAGEMENT 489.4
NYPD SCHOOL SAFETY 279.6
NYPD ADMINISTRATION-PERSONNEL 263.9
When I plot this with:
data.plot(x='Division',y='Expenditures ($000,000)')
I am thrown:
KeyError: 'Division'
I am confused because my index is named 'Division' so why would I be thrown this error? Using data.plot(kind='bar') gives me a reasonable plot, but I am having an issue calling this column by its key.
Full code below:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
NYPD_data = "D:/CSVs/NYPD_Spending.csv"
data = pd.read_csv(NYPD_data, index_col = 1)
plt.figure(1, figsize=(10,10))
plt.xlabel('NYPD Division')
data.plot(x='Division', y='Expenditures ($000,000)')
plt.show()
data = {'Agency': {0: 'NYPD', 1: 'NYPD', 2: 'NYPD', 3: 'NYPD'},
'Division': {0: 'OPERATIONS',
1: 'EXECUTIVE MANAGEMENT',
2: 'SCHOOL SAFETY',
3: 'ADMINISTRATION-PERSONNEL'},
'Expenditures ($000,000)': {0: 3331.0, 1: 489.4, 2: 279.6, 3: 263.9}}
df = pd.DataFrame.from_dict(data)
plt.figure(figsize=(10, 10))
plt.plot(df['Division'], df['Expenditures ($000,000)'])
plt.xticks(rotation='30')
plt.xlabel('NYPD Division')
plt.show()
Produces Plot:
df.plot('Division', 'Expenditures ($000,000)')
df.plot(x='Division', y='Expenditures ($000,000)')
Does not work:
df.plot(x=df['Division'], y=['Expenditures ($000,000)'])
This doesn't work because the source is already specified by df.plot
. x
& y
should be x : label or position, default None
and y : label, position or list of label, positions, default None
respectively. So the issue is sending parameters to the method in an incorrect form. pandas.DataFrame.plot
In the case of:
df = pd.read_csv('Book1.csv', index_col=1)
df =
Agency Expenditures ($000,000)
Division
OPERATIONS NYPD 3331.0
EXECUTIVE MANAGEMENT NYPD 489.4
SCHOOL SAFETY NYPD 279.6
ADMINISTRATION-PERSONNEL NYPD 263.9
Note that Division
is now the index and no longer a column so using x = 'Division'
will not work.
df.plot()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.