7

I am using Python's csv.DictReader to read in values from a CSV file to create a dictionary where keys are first row or headers in the CSV and other rows are values. It works perfectly as expected and I am able to get a dictionary, but I only want certain keys to be in the dictionary rather than all of the column values. What is the best way to do this? I tried using csv.reader but I don't think it has this functionality. Maybe this can be achieved using pandas?

Here is the code I was using with CSV module where Fieldnames was the keys that I wanted to retain in my dict. I realized it isn't used for what I described above.

import csv
with open(target_path+target_file) as csvfile:
    reader = csv.DictReader(csvfile,fieldnames=Fieldnames)
    for i in reader:
        print i
3
  • 1
    pandas.DataFrame.to_dict Commented Jun 4, 2017 at 18:21
  • @not_a_robot i want to retain only some keys in final dict. Commented Jun 4, 2017 at 18:23
  • try to use "pandas.read_csv" method with parameter "usecols" Commented Jun 4, 2017 at 18:28

2 Answers 2

14

You can do this very simply using pandas.

import pandas as pd

# get only the columns you want from the csv file
df = pd.read_csv(target_path + target_file, usecols=['Column Name1', 'Column Name2'])
result = df.to_dict(orient='records')

Sources:

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

8 Comments

thanks for reply but is the end result a list of dicts? i wanted to convert each row into a dict with with some keys. let me try this . thanks :)
It didn't before, but the updated answer should work for you once you change the column names to be what you have in your data.
can i also rename some of the existing key names like i provide a mapping between old keys and new keys?
Here's a SOA for renaming existing key names stackoverflow.com/questions/11346283/…
thanks again just one last thing there are some columns with empty values in csv file so in dictionary it become nan .is there any way to make hem default to 0 for empty cells in csv. should i ask a seperate question for it?
|
5

You can use the to_dict method to get a list of dicts:

import pandas as pd

df = pd.read_csv(target_path+target_file, names=Fieldnames)

records = df.to_dict(orient='records')

for row in records:
    print row

to_dict documentation:

In [67]: df.to_dict?
Signature: df.to_dict(orient='dict')
Docstring:
Convert DataFrame to dictionary.

Parameters
----------
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
    Determines the type of the values of the dictionary.

    - dict (default) : dict like {column -> {index -> value}}
    - list : dict like {column -> [values]}
    - series : dict like {column -> Series(values)}
    - split : dict like
      {index -> [index], columns -> [columns], data -> [values]}
    - records : list like
      [{column -> value}, ... , {column -> value}]
    - index : dict like {index -> {column -> value}}

      .. versionadded:: 0.17.0

    Abbreviations are allowed. `s` indicates `series` and `sp`
    indicates `split`.

Returns
-------
result : dict like {column -> {index -> value}}
File:      /usr/local/lib/python2.7/dist-packages/pandas/core/frame.py
Type:      instancemethod

5 Comments

thanks a lot for your reply wish i could accept more than one answer.but can you please tell if there is any way that i can rename some existing keys by providing a mapping or something?
The names attribute allows you to provide your own column names. If your file contains a header, just include header=0 (i.e. 1st line) to make sure you replace the header with your own mappings in names. In addition, if you wish to only keep specific columns, you can specify them in the usecols parameter.
i want to keep specefic columns but after that rename those columns
That's exactly what the names parameter allows you to do. Refer to pandas.read_csv documentation for details and play around with it to get a better grasp of its features.
looking into it

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.