I've a datafram
inputfile UserId UserGroup status
0 samlefile1 user1@gmail.com xyz pass
1 testfile user2@gmail.com abc pass
2 example user3@gmail.com xyz pass
3 example user3@gmail.com xyz fail
so I'm trying to select each item in 'inputfile' column and check how many times it is passed in 'status' column and convert to json 'numberOfTimesSuccess':1
{inputfile:samplefile1, UserId:user1@gmail.com, UserGroup:xyz, status:pass, numberofTimesuccess:1}
{inputfile:testfile, UserId:user2@gmail.com, UserGroup:xyz, status:pass, numberofTimesuccess:1}
{inputfile:example, UserId:user3@gmail.com, UserGroup:xyz, status:pass, numberofTimesuccess:1}
{inputfile:example2, UserId:user3@gmail.com, UserGroup:xyz, status:"", numberofTimesuccess:1}
Here is my python code:
filt = df['inputfile'] = 'samplefile1'
success = df[filt]['status'].str.contains('pass').sum
newdict={}
newdict['numberOfTimesSuccess'] = success
output:
{numberOfTimesSuccess : 1 }
I'm only getting output for one item in a column but I want for all the items in a column and convert to json.
First you need to Count # of success
per each InputFile
then merge it with base dataframe. Then use df.to_json
with orient='records'
to convert each row to JSON.
import json
df = pd.DataFrame([
['samlefile1','user1@gmail.com', 'xyz' ,'pass'],
['samlefile1','user5@gmail.com', 'xyz' ,'pass'],
['samlefile1','user6@gmail.com', 'xyz' ,'pass'],
['testfile','user2@gmail.com', 'abc' ,'pass'],
['example','user3@gmail.com', 'xyz' ,'pass'],
['example','user3@gmail.com', 'xyz' ,'fail']],columns = ['InputFile','UserId', 'UserGroup' ,'status']
)
#Count no of status per filename
Input_status_count = df.groupby(['InputFile']).agg(success_count=('status', lambda x: x[x == 'pass'].count()),
fail_count=('status', lambda x: x[x == 'fail'].count()))
#Merge the 2 files on 'InputFile' column
FinalDF = pd.merge(df, Input_status_count,on = "InputFile" )
JSON_String = FinalDF.to_json(orient='records')
JSON_String
Output:
[
{"InputFile":"samlefile1","UserId":"user1@gmail.com","UserGroup":"xyz","status":"pass","success_count":3,"fail_count":0},
{"InputFile":"samlefile1","UserId":"user5@gmail.com","UserGroup":"xyz","status":"pass","success_count":3,"fail_count":0},
{"InputFile":"samlefile1","UserId":"user6@gmail.com","UserGroup":"xyz","status":"pass","success_count":3,"fail_count":0},
{"InputFile":"testfile","UserId":"user2@gmail.com","UserGroup":"abc","status":"pass","success_count":1,"fail_count":0},
{"InputFile":"example","UserId":"user3@gmail.com","UserGroup":"xyz","status":"pass","success_count":1,"fail_count":1},
{"InputFile":"example","UserId":"user3@gmail.com","UserGroup":"xyz","status":"fail","success_count":1,"fail_count":1}
]
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.