Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将以特定方式替换字符串的函数应用于python dataframe系列_Python_Pandas_Apply - Fatal编程技术网

如何将以特定方式替换字符串的函数应用于python dataframe系列

如何将以特定方式替换字符串的函数应用于python dataframe系列,python,pandas,apply,Python,Pandas,Apply,我试图想出一个函数来代替字符串 带有“ORG\u CD\u XXX”的“ORG\u CD\u XXX>0.00” 此字符串可以出现在给定数据帧行中的任何位置。我正试图想出一个函数来进行替换,但当我尝试将其应用于数据帧系列时,我得到了错误indexer:list index超出范围 import pandas as pd data = {'Rule': ['HAD_MAA_PM and HAD_MAA_PM and ACH_PERC_PM > 66.64 a

我试图想出一个函数来代替字符串 带有“ORG\u CD\u XXX”的“ORG\u CD\u XXX>0.00”

此字符串可以出现在给定数据帧行中的任何位置。我正试图想出一个函数来进行替换,但当我尝试将其应用于数据帧系列时,我得到了错误
indexer:list index超出范围

import pandas as pd
data = {'Rule': 
                ['HAD_MAA_PM and HAD_MAA_PM and ACH_PERC_PM > 66.64 and ACH_PERC_CM > 82.19'
                 ,'HAD_MAA_PM and HAD_MAA_PM and ORG_CD_DDV > 0.00 and ACH_PERC_CM > 82.19'
                 ,'HAD_MAA_PM and HAD_MAA_PM and ORG_CD_DDV > 0.00 and ach_perc_chg_CM <= 0.00 and ACH_PERC_PPM > 48.99']
                }
df=pd.DataFrame.from_dict(data)

def org_cd_replace(text):
    text1=text.split('ORG_CD_')
    text2=[item.replace(' > 0.00',"",1) for item in text1]
    text3=text2[0]+'ORG_CD_'+text2[1]

    return text3

df['Rule'].apply(lambda x:org_cd_replace(x))
将熊猫作为pd导入
数据={'Rule':
['HAD_MAA_PM和HAD_MAA_PM和ACH_PERC_PM>66.64和ACH_PERC_CM>82.19'
“HAD_MAA_PM和HAD_MAA_PM和ORG_CD_DDV>0.00和ACH_PERC_CM>82.19”
,“HAD_MAA_PM和HAD_MAA_PM和ORG_CD_DDV>0.00和ach_perc_chg_CM 48.99”]
}
df=pd.DataFrame.from_dict(数据)
def组织cd替换(文本):
text1=text.split('ORG\u CD\u'))
text2=[item.replace('>0.00','',1)用于text1中的项]
text3=text2[0]+'ORG\u CD\'+text2[1]
返回文本3
df['Rule'].应用(lambda x:org\u cd\u replace(x))

您的代码很好。这里的问题是因为某些字符串中没有
'ORG\u CD'
。 要解决此问题,只需添加一个测试:

def org_cd_replace(text):
    if 'ORG_CD_' in text:
        text1=text.split('ORG_CD_')
        text2=[item.replace(' > 0.00','', 1) for item in text1]
        text3=text2[0]+'ORG_CD_'+text2[1]
        return text3
    return text

df = pd.DataFrame(df['Rule'].apply(lambda x:org_cd_replace(x)))
在应用lambda函数后,必须在末尾添加pd.DataFrame()调用以获取数据帧。
毫无疑问,有更好的方法(至少效率更高)可以做到这一点

您的代码很好。这里的问题是因为某些字符串中没有
'ORG\u CD'
。 要解决此问题,只需添加一个测试:

def org_cd_replace(text):
    if 'ORG_CD_' in text:
        text1=text.split('ORG_CD_')
        text2=[item.replace(' > 0.00','', 1) for item in text1]
        text3=text2[0]+'ORG_CD_'+text2[1]
        return text3
    return text

df = pd.DataFrame(df['Rule'].apply(lambda x:org_cd_replace(x)))
在应用lambda函数后,必须在末尾添加pd.DataFrame()调用以获取数据帧。
毫无疑问,有更好的方法(至少效率更高)可以做到这一点

谢谢你,查尔斯,解决办法就在我眼皮底下。谢谢你把它指给我。谢谢你,查尔斯,解决办法就在我眼皮底下。谢谢你把它指给我看。