0

I am wondering if there is a simple and intuitive way to do something like:

Select * From CSV File Where

I searched the web, but I couldn't find any SQL-type syntax that queries a CSV file. In R, I can do this:

require(sqldf)
df <- read.csv.sql("C:\\your_path_here\\CSV1.csv", "select * from file where Name='Ryan'")
df

I am wondering if there is something similar in Python.

aydow
  • 3,673
  • 2
  • 23
  • 40
ASH
  • 20,759
  • 19
  • 87
  • 200

1 Answers1

7

Load the CSV into a pandas DataFrame and then you can use pandasql to perform SQL queries on your data

import pandas as pd
import pandasql as ps

df = pd.read_csv('path/to/csv')
df1 = ps.sqldf('select * from df where x>=10', locals())
aydow
  • 3,673
  • 2
  • 23
  • 40
  • Thanks so much aydow. This is exactly what I was looking for. Thanks to everyone else. Everything was helpful, and I saved all these solutions to my personal library for future reference. – ASH Jul 30 '18 at 02:50