searching for a value in csv and returning the row multiple times

10,198

Solution 1

You can use Pandas as follows:

csv file:

Id,Name,Age
1,John,30
2,Alex,20
3,Albert,30
4,Richard,30
5,Mariah,30

python:

import pandas as pd

df = pd.read_csv("ex.csv", sep = ",")
print df[df["Age"] == 30] 

   Id     Name  Age
0   1     John   30
2   3   Albert   30
3   4  Richard   30
4   5   Mariah   30

Solution 2

You can use the pandas module which is made for processing tabular data.

First: read your csv into a so called DataFrame:

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

Now you can filter the rows that you need by logical indexing:

result = df[df['Age']==23]

To get the result back onto disk just use the to_csv method:

result.to_csv('result.csv')
Share:
10,198
scott.turner
Author by

scott.turner

Updated on June 18, 2022

Comments

  • scott.turner
    scott.turner almost 2 years

    i'm a noob trying to learn python,

    i am trying to write a script for a CSV file that has 30,000 rows of data.

    i would like to look through every row for a number in a column and return the row every time it finds that number.

    i have searched and tried many different suggestion and they don't seem to do what i need it to can anyone help me, if i'm not making sense please let me know.

    here is what i have so far and it is only returning to headers:

        import csv
    
    with open("test.csv", "r") as input, open ("result.txt","w") as result:
              testfilereader = csv.DictReader(input) 
              Age = 23
              fieldnames = testfilereader.fieldnames
              testfilewriter = csv.DictWriter(result, fieldnames, delimiter=',',)
              testfilewriter.writeheader()      
              for row in testfilereader:
                     for field in row:
                            if field == Age:
                                 testfilewriter(row)
    
    
    input.close
    

    thanks all

  • scott.turner
    scott.turner about 7 years
    hi thank you all for the help but if i use pandas how would i get into a txt file or can i not do this
  • Sebastian Wozny
    Sebastian Wozny about 7 years
    is a csv file fine as well?
  • scott.turner
    scott.turner about 7 years
    yeah thats fine as well
  • Sebastian Wozny
    Sebastian Wozny about 7 years
    If my answer answered your question, please accept it.
  • scott.turner
    scott.turner about 7 years
    hi i have been trying to install pandas and i have installed anaconda and the pandas module is not showing do you know a better way for me to get the panda module on thanks again
  • scott.turner
    scott.turner over 6 years
    hi,can you tell me how i would search for multiple values in the age column