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?
-
What are you using to read/write the csv's ?J.C– J.C2020-04-09 15:11:20 +00:00Commented Apr 9, 2020 at 15:11
-
The csv python moduleKaiger Chainer– Kaiger Chainer2020-04-09 15:13:03 +00:00Commented 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.J.C– J.C2020-04-09 15:19:12 +00:00Commented 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 CSVJ.C– J.C2020-04-09 15:19:47 +00:00Commented Apr 9, 2020 at 15:19
Add a comment
|
1 Answer
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')