Joseph Seung Jae Dollar
Joseph Seung Jae Dollar

Reputation: 1066

Python Pandas printing out values of each cells

I am trying to fetch values from an excel file using pandas dataframe and print out the values of each cells. Im using read_excel() to populate the dataframe, and I am looking for specific rows using the following line of code:

df.loc[df['Parcel_ID'] == parcel]

parcel being an arbitrary input from the user. And I simply use this to print out the cells:

row = df.loc[df['Parcel_ID'] == parcel]

print row['Category']
print row['Sub_Category']
.
.
(more values)

What I want is only the values from the cells, yet I get dtypes, names of the column, and other junks that I don't want to see. How would I only print out the value from each cells?

Upvotes: 9

Views: 76973

Answers (3)

Karam Qusai
Karam Qusai

Reputation: 799

How to find a value in a column (Column 1 name) by another value in another column (Column 2 name)

    df = pd.read_excel('file.xlsm','SheetName') #Get a excel file sheet

Then you have two ways to get that:

First Way:

    Value_Target = df.loc[df['Column1name']== 'Value_Key']['Column2name'].values

Second Way:

    Value_Target = df['Column1name'][df['Column2name']=='Value_Key'].values[0]

Value_Key is the value in a column you have Value_Target is the value you want to find using Value_Key

Upvotes: 0

Anton Protopopov
Anton Protopopov

Reputation: 31672

If you have several values in your row you could use following:

row['Category'].values.tolist()
row['Sub_Category'].values.tolist()

Upvotes: 19

EdChum
EdChum

Reputation: 394051

IIUC the following should work:

print row['Category'][0]
print row['Sub_Category'][0]

what is returned will be a Series in your case a Series with a single element which you index into to return a scalar value

Upvotes: 4

Related Questions