1

I want to read a CSV using Pandas but only certain columns and only rows with spicific values. for example I have a csv of "people and their heights", I want to read the "name" column and "height" column of people that are > "160cm" height only. I want to do this in the first step of read_csv() not after loading it.

import pandas as pd
cols = ['name','height']
df = pd.read_csv("people_and_heights.csv", usecols=cols)

so I want to add a condition to read rows with certain values only or rows that doesn't have nulls for example.

7
  • why not apply those filters and export it into new csv? Commented Jan 16, 2022 at 21:28
  • 1
    Are you doing this for the purpose of reducing memory usage, or some other reason? Commented Jan 16, 2022 at 21:39
  • 2
    If this is a memory issue as @NickODell is suggesting, your best bet is probably to load the .csv in chunks. I don't think you can avoid loading the rows you don't want as they will need to be loaded to check if they meet your criteria. Commented Jan 16, 2022 at 21:49
  • exactly @NickODell . I'm thinking to overcome memory issues as I'm dealing with more than 50GB zipped CSV's and many of the rows are useless since it's null or with values that are byonde the scope of my analysis. Commented Jan 16, 2022 at 22:01
  • @Pepsi-Joe that would be my last resort really, especially with many CSV's to deal with. Commented Jan 16, 2022 at 22:05

1 Answer 1

0

How about this?:

import pandas as pd
from io import StringIO

with open("people_and_heights.csv") as file:
    colNames = "\"col1\",\"name\",\"col3\",\"height\""
    filteredCsv = "\n".join([colNames,"".join([line for index,line in enumerate(file) if index != 0 and int(line.split(',')[3]) >= 165])])

df = pd.read_csv(StringIO(filteredCsv),usecols=["name","height"])
Sign up to request clarification or add additional context in comments.

1 Comment

ah right :) I only thought about "I want to do this in the first step of read_csv() not after loading it." part. I didn't notice the comments sorry :)

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.