Kaiger Chainer
Kaiger Chainer

Reputation: 33

Write specifics rows with specific column values in python csv

I have a csv file and I need to write lines with a common value in one of the columns in another csv file, how can I do this?

Upvotes: 0

Views: 2264

Answers (1)

FAHAD SIDDIQUI
FAHAD SIDDIQUI

Reputation: 641

As I understand your problem, you want to read csv file, then based on a condition on any column's value you want to filter it and finally write into a csv file. If I am correct then you can do the following :

#Import pandas

import pandas as pd

#Read your csv file as pandas dataframe
df = pd.read_csv("your_csv_file_name")

#Apply filter condition
df = df[df['Column_name_for_fitering'] == "Value_for_filtering"]

# Save as new csv file
df.to_csv('your_output_file_name')

Upvotes: 4

Related Questions