如果有条件如何应用并应用于数据框

时间:2019-12-22 14:53:16

标签: python pandas

需要用'detected'bool3_res res的键is_doc1检查bool_res的{​​{1}}键

  1. 如果bool_2,那么我的bool3_res['detected'] == bool1_res['is_doc1'] == True必须返回

  2. 如果resp,那么我的bool3_res['detected'] == bool2_res['is_doc1'] == True必须返回\

3:否则返回“无效”

数据框

resp

我的代码

user_uid,bool1,bool2,bool3,bool1_res,bool2_res,bool3_res
1001,27452.webp,981.webp,d92e.webp,"{'is_doc1': False, 'is_doc2': True}","{'is_doc1': True, 'is_doc2': True}","{'detected': True, 'count': 1}"
1002,27452.webp,981.webp,d92e.webp,"{'is_doc1': True, 'is_doc2': True}","{'is_doc1': False, 'is_doc2': True}","{'detected': True, 'count': 1}"

我的期望值

df ['new']

def new_func(x):
    d1 = df['bool1_res'].to_dict()
    d1 = eval(d1[0])
    d2 = df['bool2_res'].to_dict()
    d2 = eval(d2[0])
    d3 = df['bool3_res'].to_dict()
    d3 = eval(d3[0])

    if d1['is_doc1'] == d3['detected'] == True:
        resp = {
            "task_id": "uid",
            "group_id": "uid",
            "data": {
            "document1": df['bool1'],
            "document2": df['bool3']
            }
            }

    elif d2['is_doc1'] == d3['detected'] == True:
        resp = {
            "task_id": "user_uid",
            "group_id": "uid",
            "data": {
            "document1": df['bool2'],
            "document2": df['bool3']
            }
            }
    elif d3['detected'] == False:
        resp = 'Not valid'
    else:
        resp = 'Not valid'
    return resp
df['new'] = df.apply(new_func, axis = 1)
#df['new'] = df[['bool1', 'bool2', 'bool3', 'bool1_res', 'bool2_res', 'bool3_res']].applymap(new_func)

我的出场 df ['new']

{'u_id': 'uid', 'group': 'uid', 'data': {'document1': ['981.webp'], 'document2': {'d92e.webp'}}}"
{'u_id': 'uid', 'group': 'uid', 'data': {'document1': ['27452.webp'], 'document2': {'d92e.webp'}}}"

2 个答案:

答案 0 :(得分:0)

您应避免使用eval,而应将xdf一起使用,而将[]x['bool1']一起使用,以对每一行进行处理,并为一个元素列表将x['bool2']添加到{{1 }},x['bool3']import ast def new_func(x): d1 = ast.literal_eval(x['bool1_res']) d2 = ast.literal_eval(x['bool2_res']) d3 = ast.literal_eval(x['bool3_res']) if d1['is_doc1'] == d3['detected'] == True: resp = { "task_id": "uid", "group_id": "uid", "data": { "document1": [x['bool1']], "document2": [x['bool3']] } } elif d2['is_doc1'] == d3['detected'] == True: resp = { "task_id": "user_uid", "group_id": "uid", "data": { "document1": [x['bool2']], "document2": [x['bool3']] } } elif d3['detected'] == False: resp = 'Not valid' else: resp = 'Not valid' return resp df['new'] = df.apply(new_func, axis = 1)

print (df['new'].iat[0])
{'task_id': 'user_uid', 'group_id': 'uid', 'data': {'document1': ['981.webp'], 'document2': ['d92e.webp']}}

print (df['new'].iat[1])
{'task_id': 'uid', 'group_id': 'uid', 'data': {'document1': ['27452.webp'], 'document2': ['d92e.webp']}}

function onLoad() {
    logT("onLoad function called...");
    document.body.style.background = 'green'
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
    logT("Device ready function called...");
    loadUserTrackData();
}

function loadUserTrackData(){
    
    cordova.exec(function(jsonObj) {
                 logT("Success -- loadUserTrackData");
                 if(jsonObj.length > 0){
                 var getContact = JSON.parse(jsonObj);
                 UserTrackData.CMI._id = getContact.id;
                 UserTrackData.CMI._name = getContact.name;
                 UserTrackData.CMI._lesson_status = getContact.lessonStatus;
                 UserTrackData.CMI._entry = getContact.entry;
                 UserTrackData.CMI._credit = getContact.credit;
                 UserTrackData.CMI._score_raw = getContact.scoreRaw;
                 UserTrackData.CMI._score_min = getContact.scoreMin;
                 UserTrackData.CMI._score_max = getContact.scoreMax;
                 UserTrackData.CMI._laucnh_data = getContact.launchData;
                 UserTrackData.CMI._lession_location = getContact.lessonLocation;
                 UserTrackData.CMI._lesson_mode = getContact.lessonMode;
                 UserTrackData.CMI._exit = getContact.exit;
                 UserTrackData.CMI._session_time = getContact.sessionTime;
                 UserTrackData.CMI._suspend_data = getContact.suspendData;
                 UserTrackData.CMI._total_time = getContact.totalTime;
                 UserTrackData.CMI._selected_position = getContact.position;
                 
                 }
                 cordova.exec(function(url) {logT("Success");
                              var objTag = document.getElementById("serverData");
                              
                              
                              if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".MOV")){
                              objTag.innerHTML = '<video width="100%" height="100%" autoplay controls controlsList="nodownload nofullscreen" poster="orange_rectangle.png"><source src=' + url + ' type="video/mp4">This browser does not support HTML5 video.</video>';
                              }
                              
                              else if(url.toLowerCase().endsWith(".mp3") || url.endsWith(".m4a") || url.endsWith(".wav")){
                              objTag.innerHTML = '<audio controls><source src=' + url + '>Your browser does not support audio element.</video>';
                              }
                              
                              else if(url.toLowerCase().endsWith(".pdf") || url.toLowerCase().endsWith(".doc")
                                       || url.toLowerCase().endsWith(".docx") || url.toLowerCase().endsWith(".xls")
                                       || url.toLowerCase().endsWith(".xlsx") || url.toLowerCase().endsWith(".ppt") || url.toLowerCase().endsWith(".pptx")){
                              objTag.innerHTML = '<object type="text/html" data=https://view.officeapps.live.com/op/view.aspx?src=' + url+ '></object>';
                              }
                              
                              //scorm play path
                              else{
                                objTag.innerHTML = '<object type="text/html" data=' + url + '></object>';
                              }
                              
                              },
                              function(e) {logT("Error: "+e);},
                              "JCommunicatorPlugin", "loadPackageURL",["", "file:///android_asset/www/samples/RuntimeBasicCalls_SCORM12/shared/launchpage.html", "" || {}]);},
                 function(e) {logT("Error: "+e);},
                 "JCommunicatorPlugin", "loadUserTrackData",["", "", "" || {}]);
}
//logT("Loading: "+url)
//file:///android_asset/www/samples/RuntimeBasicCalls_SCORM12/shared/launchpage.html
//iFrame height
//objTag.innerHTML = '<object type="text/html" data=' + url + '></object>';
//objTag.setAttribute('data', url);
function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}

function logT(text){
    cordova.exec(function() {},
                 function(e) {console.log("Error: "+e);},
                 "JCommunicatorPlugin", "log", ["", text, "" || {}]);
}


function insertScoElement(elementName, elementValue, position){
    cordova.exec(function() {logT("got success result from insertScoElement");},
                 function(e){logT("Error: "+e);},
                 "JCommunicatorPlugin", "setSco", [elementName, elementValue, "" || {}]);
    
    if (elementName == JSConstants.CMI.LessonStatus &&
        (elementValue.toLowerCase() == "completed" ||
         elementValue.toLowerCase() == "passed" )) {
        logT("status track blocked as this is already completed");
        cordova.exec(function() {logT("got success result from insertSco Completion status");},
                     function(e){logT("Error: "+e);},
                     "JCommunicatorPlugin", "setScoAsComplete", [position, elementValue, "" || {}]);
    }
}

function getScoElementValue(elementName){
    cordova.exec(function(elementVal) {logT("got success result from getScoElementValue");},
                 function(e) {logT("Error: "+e);},
                 "JCommunicatorPlugin", "getSco", [elementName, "", "" || {}]);
}

function scoInit(){
    cordova.exec(function() {logT("got success result from scoInit");},
                 function(e){logT("Error: "+e);},
                 "JCommunicatorPlugin", "init", ["", "", "" || {}]);
}

function scoCommit(strEmpty){
    cordova.exec(function() {logT("got success result from scoCommit");},
                 function(e){logT("Error: "+e);},
                 "JCommunicatorPlugin", "commit", [strEmpty, "", "" || {}]);
}

function scoFinish(){
    cordova.exec(function() {logT("got success result from scoFinish");},
                 function(e) {logT("Error: "+e);},
                 "JCommunicatorPlugin", "finish", ["", "", "" || {}]);
}


var JSConstants = {
CMI:
    {
    Entry: "cmi.core.entry",
    Id: "cmi.core.student_id",
    Name: "cmi.core.student_name",
    LessonStatus: "cmi.core.lesson_status",
    None: "cmi.core.none",
    Location: "cmi.core.lesson_location",
    Credit: "cmi.core.credit",
    SessionTime: "cmi.core.session_time",
    TotalTime: "cmi.core.total_time",
    ScoreRaw: "cmi.core.score.raw",
    ScoreMin: "cmi.core.score.min",
    ScoreMax: "cmi.core.score.max",
    LessonMode: "cmi.core.lesson_mode",
    Exit: "cmi.core.exit",
    CmiObjectives: "cmi.objectives",
    CmiInteractions: "cmi.interactions",
    CmiInteractionsCount: "cmi.interactions._count",
    CmiLDChildren: "cmi.launch_data._children",
    CmiLDCount: "cmi.launch_data._count",
    CmiLData: "cmi.launch_data",
    CmiSuspendData: "cmi.suspend_data",
    CmiLDCount: "cmi.launch_data._count",
    CmiObjectiveChildren: "cmi.objectives._children",
    CmiObjectiveCount: "cmi.objectives._count",
    CmiInteractionsChildren: "cmi.interactions._children",
    CmiInteractionsCount: "cmi.interactions._count",
    CmiStudentDataMasteryScore: "cmi.student_data.mastery_score",
    CmiSpAudio: "cmi.student_preference.audio",
    CmiSpSpeed: "cmi.student_preference.speed",
    CmiSpText: "cmi.student_preference.text",
    CmiComments: "cmi.comments"
    }
}

var UserTrackData = {
CMI:{
_id: "",
_name: "",
_credit: "",
_entry: "",
_exit: "",
_laucnh_data: "",
_lession_location: "",
_lesson_mode: "",
_lesson_status: "",
_score_max: "",
_score_min: "",
_score_raw: "",
_session_time: "",
_suspend_data: "",
_total_time: "",
_selected_position: 0
}
}

function printReturningData(strElementName){
    
    if (strElementName == JSConstants.CMI.Id) {                             logT("sco input : " + UserTrackData.CMI._id);
    } else if (strElementName == JSConstants.CMI.Name) {                   logT("sco input : " + UserTrackData.CMI._name);
    } else if (strElementName == JSConstants.CMI.LessonStatus) {           logT("sco input : " + UserTrackData.CMI._lesson_status);
    } else if (strElementName == JSConstants.CMI.Entry){                   logT("sco input : " +  UserTrackData.CMI._entry);
    } else if (strElementName == JSConstants.CMI.None){                    logT("sco input : " +  "");
    } else if (strElementName == JSConstants.CMI.Credit){                  logT("sco input : " +  UserTrackData.CMI._credit);
    } else if (strElementName == JSConstants.CMI.ScoreRaw){                logT("sco input : " +  UserTrackData.CMI._score_raw);
    } else if (strElementName == JSConstants.CMI.ScoreMin){                logT("sco input : " +  UserTrackData.CMI._score_min);
    } else if (strElementName == JSConstants.CMI.ScoreMax){                logT("sco input : " +  UserTrackData.CMI._score_max);
    } else if (strElementName == JSConstants.CMI.CmiLData){                logT("sco input : " +  UserTrackData.CMI._laucnh_data);
    } else if (strElementName == JSConstants.CMI.Location){                logT("sco input : " +  UserTrackData.CMI._lession_location);
    } else if (strElementName == JSConstants.CMI.LessonMode){              logT("sco input : " +  UserTrackData.CMI._lesson_mode);
    } else if (strElementName == JSConstants.CMI.Exit){                    logT("sco input : " +  UserTrackData.CMI._exit);
    } else if (strElementName == JSConstants.CMI.SessionTime){             logT("sco input : " +  UserTrackData.CMI._session_time);
    } else if (strElementName == JSConstants.CMI.CmiSuspendData){          logT("sco input : " +  UserTrackData.CMI._suspend_data);
    } else if (strElementName == JSConstants.CMI.TotalTime){               logT("sco input : " +  UserTrackData.CMI._total_time);
    } else if (strElementName == JSConstants.CMI.CmiLDChildren){           logT("sco input : " +  ""); // unknown.. copied from lms
    } else if (strElementName == JSConstants.CMI.CmiLDCount){              logT("sco input : " +  ""); // unknown.. copied from lms
    } else if ((strElementName.indexOf(JSConstants.CMI.CmiObjectives) >= 0) ||
               (strElementName.indexOf(JSConstants.CMI.CmiInteractions) >= 0)){
        logT("sco input : " +  ""); // unknown.. copied from lms
    }else {                                                                logT("sco input : " +  ""); // unknown.. copied from lms
    }
}

if (!String.prototype.endsWith) {
    String.prototype.endsWith = function(search, this_len) {
        if (this_len === undefined || this_len > this.length) {
            this_len = this.length;
        }
        return this.substring(this_len - search.length, this_len) === search;
    };
}

答案 1 :(得分:0)

我假设这是扩展代码行后数据的样子:(此外,即使您只添加一些空格也很容易阅读... ^ _ ^)

df = pd.DataFrame(
    [
        [1001, "27452.webp", "981.webp", "d92e.webp",
            "{'is_doc1': False, 'is_doc2': True}",
            "{'is_doc1': True, 'is_doc2': True}",
            "{'detected': True, 'count': 1}"
        ],
        [1002, "27452.webp", "981.webp", "d92e.webp",
            "{'is_doc1': True, 'is_doc2': True}",
            "{'is_doc1': False, 'is_doc2': True}",
            "{'detected': True, 'count': 1}"
        ],
        [1003, "27452.webp", "981.webp", "d92e.webp",
            "{'is_doc1': True, 'is_doc2': True}",
            "{'is_doc1': False, 'is_doc2': True}",
            "{'detected': False, 'count': 1}"
        ],
    ],
    columns=['user_uid', 'bool1', 'bool2', 'bool3', 'bool1_res', 'bool2_res',
             'bool3_res'
    ]
)

我的答案

执行过程分为两部分:(1)解析字符串和(2)处理/创建“新”列值。

# required packages
import ast
import pandas as pd
# for type suggestions
from typing import Any

第1部分:解析字典字符串

此功能通过pd.DataFrame.applymap应用于数据框中的每个元素,并使用ast.literal_eval,如@jezrael正确建议的那样。

def str2dict(x: Any):
    """(Step 1) Parses argument using ast.literal_eval"""
    try:
        x = ast.literal_eval(x.strip())

    # if x is not parsable, return x as-is
    except ValueError as e:
        pass

    finally:
        return x

第2部分:处理您的数据(即创建“新” col)

此功能应用于数据框的每一行(通过pd.DataFrame.agg):

根据您发布的函数中的逻辑,我:

  1. 检查bool3['detected']是否为False(您的前两个条件都已检测到== True);如果是这样,请引发ValueError

  2. 检查bool1的is_doc1是否为True,否则bool2的is_doc1为True

  3. 如果is_doc1都不为True,请加注ValueError

def make_newcol_entry(x: pd.Series):
    """(Step 2) constructs "new" column value for pandas group"""
    try:
        if x.bool3_res['detected'] is False:
            raise ValueError
        # check is_doc1 properties
        elif x.bool1_res['is_doc1'] is True:
            document1 = x.bool1
        elif x.bool2_res['is_doc1'] is True:
            document1 = x.bool2
        else:
            raise ValueError
    except ValueError:
        entry = "not valid"
        pass
    # if there is `is_doc1` that is True, construct your entry.
    else:
        entry = {
            "task_id": "uid",
            "group_id": "uid",
            "data": {"document1": document1, "document2": x.bool3}
        }

    return entry

要执行,请运行:

df = df.assign(new=lambda x: x.applymap(str2dict) \
                              .agg(make_newcol_entry, axis=1))

请注意,这会解析数据框中的所有个元素。

要仅解析{em> bool_res列,可以分两步执行:

# select and parse only res cols ('bool#_res'), then apply
df.update(df.filter(regex=r'_res$', axis=1).applymap(str2dict))
df = df.assign(lambda x: x.agg(apply_make_newcol_entry, axis=1))

结果

$ df
    user_uid    bool1   bool2   bool3   bool1_res   bool2_res   bool3_res   new
0   1001    27452.webp  981.webp    d92e.webp   {'is_doc1': False, 'is_doc2': True} {'is_doc1': True, 'is_doc2': True}  {'detected': True, 'count': 1}  {'task_id': 'uid', 'group_id': 'uid', 'data': {'document1': '981.webp', 'document2': 'd92e.webp'}}
1   1002    27452.webp  981.webp    d92e.webp   {'is_doc1': True, 'is_doc2': True}  {'is_doc1': False, 'is_doc2': True} {'detected': True, 'count': 1}  {'task_id': 'uid', 'group_id': 'uid', 'data': {'document1': '27452.webp', 'document2': 'd92e.webp'}}
2   1003    27452.webp  981.webp    d92e.webp   {'is_doc1': True, 'is_doc2': True}  {'is_doc1': False, 'is_doc2': True} {'detected': False, 'count': 1} not valid
$ df['new']
0   {'task_id': 'uid', 'group_id': 'uid', 'data': {'document1': '981.webp', 'document2': 'd92e.webp'}}
1   {'task_id': 'uid', 'group_id': 'uid', 'data': {'document1': '27452.webp', 'document2': 'd92e.webp'}}
2   not valid
Name: new, dtype: object
相关问题