3

I'm using the pandas library to load in a csv file using Python.

import pandas as pd
df = pd.read_csv("movies.csv")

I'm then checking the columns for specific values or statements, such as:

viewNum = df["views"] >= 1000
starringActorNum = df["starring"] > 3
df["title"] = df["title"].astype("str")
titleLen = df["title"].str.len() <= 10

I want to create a new csv file using the criteria above, but am unsure how to do that as well as how to combine all those attributes into one csv.

Anyone have any ideas?

2
  • You want to have your data frame filtered with all of the constraints you present there being applied in parallel? Commented Feb 11, 2017 at 20:30
  • Yes. I have a .csv dataset I've loaded in, but want to filter out some of the content based on certain criteria (hence, the examples above). I just don't know how to translate all my constraints to a csv Commented Feb 11, 2017 at 20:33

2 Answers 2

5

Combine the boolean masks using & (bitwise-and):

mask = viewNum & starringActorNum & titleLen

Select the rows of df where mask is True:

df_filtered = df.loc[mask]

Write the DataFrame to a csv:

df_filtered.to_csv('movies-filtered.csv')

import pandas as pd

df = pd.read_csv("movies.csv")

viewNum = df["views"] >= 1000
starringActorNum = df["starring"] > 3
df["title"] = df["title"].astype("str")
titleLen = df["title"].str.len() <= 10

mask = viewNum & starringActorNum & titleLen
df_filtered = df.loc[mask]

df_filtered.to_csv('movies-filtered.csv')
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the panda.DataFrame.query() interface. It allows text string queries, and is very fast for large data sets.

Something like this should work:

import pandas as pd
df = pd.read_csv("movies.csv")

# the len() method is not available to query, so pre-calculate
title_len = df["title"].str.len()

# build the data frame and send to csv file, title_len is a local variable
df.query('views >= 1000 and starring > 3 and @title_len <= 10').to_csv(...)

Comments

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.