0

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?

4
  • What are you using to read/write the csv's ? Commented Apr 9, 2020 at 15:11
  • The csv python module Commented Apr 9, 2020 at 15:13
  • Sorry I meant once read where do you store ? My point is using numpy or pandas would make this easier. Commented Apr 9, 2020 at 15:19
  • I fail to see why you cannot just find the value, assign it to a variable and use this variable when saving your new CSV Commented Apr 9, 2020 at 15:19

1 Answer 1

4

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')
Sign up to request clarification or add additional context in comments.

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.