简体   繁体   中英

How to get the value of an object ID stored in a variable - in python

I have a schoolID variable that is an Object. This is what prints out to the console when I print it:

school._id
Out[60]: 
0    5ca588639d83cb7261b73231
Name: _id, dtype: object

However, in order for my function to work, I need it to be a string. So at the moment I am having to create another variable and type in the string value myself. Below is my function currently:

fun = '5ca588639d83cb7261b73231'
update_this = Example.query.filter_by(_id=fun).first()

I want to be able to to use the schoolID variable in the function like this:

update_this = Example.query.filter_by(_id=schoolID).first()

I have tried converting the object to a string using the str function, but that creates the whole object as a string, rather than just the ID number that I need, see below:

str(school._id)
Out[59]: '0    5ca588639d83cb7261b73231\nName: _id, dtype: object'

Does anyone know how I can get just the '5ca588639d83cb7261b73231' part of the object saved into a variable so I can use it in my function?

school._id is a series, hence your problem. You can quickly fix by school._id.iloc[0] if you are sure the required id is the first one in the series. Consider keeping school._id as a str instead of pd.Series .

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