Reputation: 23
I wrote a function that is supposed to return some values based on a dataframe, on certain conditions.
def myFun(declaration, policynumber, ID):
tempvar = df.loc[(df['Moneyamount'] == 70) & (df['ID'] == ID) & (df['Nr deklaracji'] == declaration) & (df['Nr Polisy'] == policynumber)]
if len(tempvar) == 1:
info = [declaration, 'YES', ID, tempvar['Birthdate'], tempvar['City'], tempvar['Street'], tempvar['Housenumber']]
elif len(tempvar) == 0:
info = [declaration, 'NO', ID, tempvar['Birthdate'], tempvar['City'], tempvar['Street'], tempvar['Housenumber']]
return(info)
Everything works, except the fact that the outcome is like this:
[90052,
'YES',
58041991951,
0 19-04-58
Name: Birthdate, dtype: object,
0 Warsaw
Name: City, dtype: object,
0 Bluestreet
Name: Street, dtype: object,
0 6
Name: Housenumber, dtype: object]
My task however is not only for that function to work, but also to print the outcome to .json. Problem is however, when I try to print this into a .json, it keeps the "0, Name:, dtype: object" part, and I'd like to get rid of that.
I've looked through stack and google in general, but the usual workarounds were in a situation where an entire list was of the same datatype.
Thanks in advance.
EDIT: I found a workaround:
I added
.tolist()[0]
after each of the values that refers to the dataframe, like this
info = [declaration, 'NO', ID, tempvar['Birthdate'].tolist()[0], tempvar['City'].tolist()[0], tempvar['Street'].tolist()[0], tempvar['Housenumber'].tolist()[0]]
This allowed me to receive a neat .json format. It's not the best possible solution, but it works for me here. Thanks for the answers.
Upvotes: 0
Views: 1376
Reputation: 860
If you are directly taking the desired column from filtered data like:
df[df['column_name']==value][column_name]
Try:
df[df['column_name']==value][column_name].values.tolist()
Upvotes: 1