1

I use pandas to load a csv file and want to print out data of row, here is original data

orginal data

I want to print out 'violence' data for make a bar chart, but it occuar a keyerror, here is my code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

c_data=pd.read_csv('crime.csv')
print(c_data.head())

print (c_data['violence'])

and the error

error detail

error detail

I tried use capital VIOLENCE,print (c_data['VIOLENCE']),but also failed

error detail

error detail

can someone tell me how to work it out?

1 Answer 1

2

Try the following if your data is small:

with open('crime.csv', 'r') as my_file:
    reader = csv.reader(my_file)
    rows = list(reader)
    print rows[3]

If your data is big, try this:

from itertools import islice

with open('crime.csv', 'r') as my_file:
    reader = csv.reader(my_file)
    print next(islice(reader, 3, 4))
Sign up to request clarification or add additional context in comments.

6 Comments

Ok. I get it. You want to print out a row named 'VOILENCE'. However, the print function only prints the column with its name, and not a row. Let me check and I will give you a solution.
with open(crime.csv, 'r') as csvfile: reader = csv.reader(csvfile) rows = list(reader) print (rows[3]) that is my new code, but it say name 'crime' is not defined
Type crime.csv between apostrophes. with open('crime.csv', 'r') as csvfile: ....
it works, thank you so much Sir. Btw, I have one more question, do u know how to make a bar chart or line chart base on my data
|

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.