TypeError:使用.apply更新数据框列时,字符串索引必须为整数

时间:2018-11-13 04:28:34

标签: python pandas dataframe lambda apply

我正在基于另一列中的子字符串更新一列。这是通过遍历行来完成的。

import pandas as pd

my_DestSystemNote1_string =  'ISIN=XS1906311763|CUSIP=         |CalTyp=1'
dfDest = [('DestSystemNote1', ['ISIN=XS1906311763|CUSIP=         |CalTyp=1', 
                               'ISIN=XS0736418962|CUSIP=         |CalTyp=1', 
                               'ISIN=XS1533910508|CUSIP=         |CalTyp=1', 
                               'ISIN=US404280AS86|CUSIP=404280AS8|CalTyp=1', 
                               'ISIN=US404280BW89|CUSIP=404280BW8|CalTyp=21',
                               'ISIN=US06738EBC84|CUSIP=06738EBC8|CalTyp=21',
                               'ISIN=XS0736418962|CUSIP=         |CalTyp=1',]),
         ]
# create pandas df
dfDest = pd.DataFrame.from_items(dfDest)

def findnth(haystack, needle, n):
    parts= haystack.split(needle, n+1)
    if len(parts)<=n+1:
        return -1
    return len(haystack)-len(parts[-1])-len(needle)

def split_between(input_string, 
                  start_str, start_occurence, 
                  end_str, end_occurence
                 ):
    start_index = findnth(input_string, start_str, start_occurence-1) + len(start_str)
    end_index = findnth(input_string, end_str, end_occurence-1) + len(end_str) -1
    return input_string[start_index:end_index]

dfDest['FOUND_ISIN'] = ""
dfDest['FOUND_CUSIP'] = ""
dfDest.info()
for index, row in dfDest.iterrows():
    try:
        print(row.DestSystemNote1)
        row.FOUND_ISIN = split_between(row.DestSystemNote1, "ISIN=", 1, "|", 1)
        row.FOUND_CUSIP = split_between(row.DestSystemNote1, "CUSIP=", 1, "|", 2)
        # print ('DestSystemNote1=' + row.DestSystemNote1 + " " + 'FOUND_ISIN= ' + row.FOUND_ISIN)   
        # print ('DestSystemNote1=' + row.DestSystemNote1 + " " + 'FOUND_CUSIP= ' + row.FOUND_CUSIP)   
    except:
        pass # doing nothing on exception

enter image description here

为帮助我的学习,我想做同样的事情,但使用带有lambda函数的apply方法,即更新第三列FOUND_ISIN2,但我得到TypeError: string indices must be integers

dfDest['FOUND_ISIN2'] = dfDest["DestSystemNote1"].apply(lambda x: split_between(x['DestSystemNote1'], "ISIN=", 1, "|", 1))

将示例字符串放入函数中时,它会返回一个值

dfDest['FOUND_ISIN2'] = dfDest["DestSystemNote1"].apply(lambda x: split_between('ISIN=XS1906311763|CUSIP= |CalTyp=1',"ISIN=", 1, "|", 1) )

因此,考虑到这一点,我尝试将DestSystemNote1转换为字符串,但错误再次出现

dfDest['FOUND_ISIN2'] = dfDest["DestSystemNote1"].apply(lambda x: split_between(x['DestSystemNote1'].astype('str'), "ISIN=", 1, "|", 1))

使用.apply时,是否将解析为函数的值转换为字符串?这到底是怎么回事?

1 个答案:

答案 0 :(得分:1)

您不需要lambdaapply。坚持熊猫,您就可以分三个步骤完成(也许也可以用更少的步骤完成):

# 1 - Create DataFrame
import pandas as pd
dfDest = pd.DataFrame.from_items(dfDest)

# 2 - String parsing
cols = ['ISIN','CUSIP', 'CalTyp'] # Define Columns
dfDest[cols] = dfDest['DestSystemNote1'].str.split('|', n=-1, expand=True) # Split Strings to columns

# 3 - Replace unwanted parts of raw data
for header in cols: # look at every column and remove its header string from the data
    dfDest[header] = dfDest[header].str.replace(header + "=", '') # and add "=" to pattern you want to remove

print dfDest

输出:

                               DestSystemNote1          ISIN      CUSIP CalTyp
0   ISIN=XS1906311763|CUSIP=         |CalTyp=1  XS1906311763                 1
1   ISIN=XS0736418962|CUSIP=         |CalTyp=1  XS0736418962                 1
2   ISIN=XS1533910508|CUSIP=         |CalTyp=1  XS1533910508                 1
3   ISIN=US404280AS86|CUSIP=404280AS8|CalTyp=1  US404280AS86  404280AS8      1
4  ISIN=US404280BW89|CUSIP=404280BW8|CalTyp=21  US404280BW89  404280BW8     21
5  ISIN=US06738EBC84|CUSIP=06738EBC8|CalTyp=21  US06738EBC84  06738EBC8     21
6   ISIN=XS0736418962|CUSIP=         |CalTyp=1  XS0736418962                 1

快乐的编码。