简体   繁体   中英

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?

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM