Home > Mobile >  CSV reading with pandas doesn't return expected value
CSV reading with pandas doesn't return expected value

Time:12-25

I've been trying to search for a specific keyword in a specific CSV column and print the value found, although when I get the print it returns the whole row.

I have tried the data.filter function command I didn't have much luck.

What I plan to do is:

  1. Get to the right column
  2. Search for a value in the specific column
  3. Print a value from another named column but not the whole row.

The code:

import pandas as pd

#list only specific columns
list_columns = ["Name", "Size","Colour"]

#read csv in the defined columns above
data = pd.read_csv("Database.csv", usecols=list_columns)

##print a specific column only
#print(data["Colour"]) 

#define keyword,and wherre/what to look for
keyword = data[data["Name"] == 'Jeans']

#print the value found
print (keyword.head())

And the output:

    Name Colour     Size
0  Jeans  Black  X-Large

I feel like I need to tell it to print only a value from a specific column instead of keyword.head() - is that correct?

CodePudding user response:

From the comments:

Use

print(*keyword["Size"], sep='\n')

If you want to store the results in a variable you can use

lst = [*keyword["Size"]]

or even better,

lst = keyword['Size'].tolist()
  • Related