1
import pandas as pd

Code 1:- 
df = pd.DataFrame([{'First':'Bill','Last':'Thompson','AcctNum':'0001','AcctValue':100},
                   {'First':'James','Last':'Winters','AcctNum':'0002','AcctValue':200},
                   {'First':'Anna','Last':'Steele','AcctNum':'0003','AcctValue':300},
                   {'First':'Sean','Last':'Reilly','AcctNum':'0004','AcctValue':400}])
account_value = df.loc[df['AcctNum'] == '0003']['AcctValue'].values[0]
print(account_value)
Output=300

Code 2:- 

df1=pd.DataFrame([{'randomcolumn':'12345'}])
number=df1.loc[:,'randomcolumn']
number=number.values
number.astype(int)
Output=array([12345])

I want to store Dataframe (df1) value into integer variable like df account_value? I have tried above code 1 sample example of what I want and code 2 is the sample which i tried but it is giving me array not value.

thanks in advance.

2 Answers 2

2

Use DataFrame.at:

number_str=df1.at[0,'randomcolumn']
print(number_str)
# '12345'

number=int(number_str)
print(number)
# 1245
Sign up to request clarification or add additional context in comments.

2 Comments

@anserv Thanks !
I am glad to help you!
1

you can also cast the series into an int using int() method.

However this will only work on a single value, if you have more you'll need to return these into an array and access them by an index value or via an iteration.

for your question above simply do :

number = int(df1.loc[:,'randomcolumn'])
print(number)
out: 12345
type(number)
out: int

3 Comments

@Skynet Glad we could he of service, if this is your accepted answer please hit the green tick so this can be closed
the optimal method to select a single value is DataFrame.at
@ansev agreed but I did mention that in my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.