如何将熊猫数据框中的字符串设置在所有行的相同位置?

时间:2018-12-17 17:30:56

标签: python pandas

我正在尝试使用pandas拆分字符串以建立CPC层次结构。这是我的数据帧df_all_new_p

                     CPC   
        0   Y10T403/4602        
        1     H02S20/00     
        2   H01L31/02168        

我正在考虑将Lv.1包含一个字母,Lv2包含两个字母,Lv3包含2-3个字母,Lv4,5,6,7,8 ..设为6-10级CPC。 '/'之后的字母

例如。

Y10T403/4602 -> Y, 10,  T, 403, 43/4, 43/46, 43/460, 43/4602
H02S20/00 ->  H, 02, S, 20, 20/0,   20/00
H01L31/02168->  H, 01,  L, 31, 31/0, 31/02, 31/021, 31/0216, 31/02168 

但是当我运行我的代码时

if df_all_new_p['CPC'].map(lambda x: x[0:7]).str.contains('/').any():
    df_all_new_p['Lv1'] = df_all_new_p['CPC'].map(lambda x: x[0:1])
    df_all_new_p['Lv2'] = df_all_new_p['CPC'].map(lambda x: x[1:3])
    df_all_new_p['Lv3'] = df_all_new_p['CPC'].map(lambda x: x[3:4])
    df_all_new_p['Lv4'] = df_all_new_p['CPC'].map(lambda x: x[4:6])
    df_all_new_p['Lv5'] = df_all_new_p['CPC'].map(lambda x: x[4:8])
    df_all_new_p['Lv6'] = df_all_new_p['CPC'].map(lambda x: x[4:9])

elif df_all_new_p['CPC'].map(lambda x: x[0:8]).str.contains('/').any():
    df_all_new_p['Lv1'] = df_all_new_p['CPC'].map(lambda x: x[0:1])
    df_all_new_p['Lv2'] = df_all_new_p['CPC'].map(lambda x: x[1:3])
    df_all_new_p['Lv3'] = df_all_new_p['CPC'].map(lambda x: x[3:4])
    df_all_new_p['Lv4'] = df_all_new_p['CPC'].map(lambda x: x[4:7])
    df_all_new_p['Lv5'] = df_all_new_p['CPC'].map(lambda x: x[7:9])
    df_all_new_p['Lv6'] = df_all_new_p['CPC'].map(lambda x: x[7:10])

else:

    df_all_new_p['Lv1'] = df_all_new_p['CPC'].map(lambda x: x[0:1])
    df_all_new_p['Lv2'] = df_all_new_p['CPC'].map(lambda x: x[1:3])
    df_all_new_p['Lv3'] = df_all_new_p['CPC'].map(lambda x: x[3:4])
    df_all_new_p['Lv4'] = df_all_new_p['CPC'].map(lambda x: x[4:8])
    df_all_new_p['Lv5'] = df_all_new_p['CPC'].map(lambda x: x[8:10])
    df_all_new_p['Lv6'] = df_all_new_p['CPC'].map(lambda x: x[8:11])


df_all_new_p


                 CPC   Lv1 Lv2 Lv3 Lv4  Lv5    Lv6
    0   Y10T403/4602        Y1  0   T4  03     4602
    1     H02S20/00     H   02  S   20  20/0   20/00
    2   H01L31/02168        H0  1   L3  1/     02168

结果表明,只有1 H02S20/00得到了正确的答案,而另一行得到了错误的结果。我认为原因是由每一行中的字符位置引起的。因此,我想知道有什么方法可以解决这个问题吗?

我希望看到这样的结果。

         CPC    1  2  3   4  5       6      
 Y10T403/4602   Y 10  T 403 43/4 43/46 
    H02S20/00   H 02  S 20  20/0 20/00
  H01L31/02168  H 01  L 31  31/0 31/02 

2 个答案:

答案 0 :(得分:2)

通过说可能有更有效的方法来进行此操作。也就是说,您可以使用str.find('/')来帮助建立索引:

df=pd.DataFrame({'a':[1,2,3],'CPC':['Y10T403/4602','H02S20/00','H01L31/02168']})


    a   CPC
0   1   Y10T403/4602
1   2   H02S20/00
2   3   H01L31/02168

[i[i.find('/')-2:i.find('/')+3] for i in df['CPC']]

['03/46', '20/00', '31/02']

因此,您可以定义要传递给df.apply()的函数

def parse_cpc(val):
    elems=[]
    elems.append(val[0])
    elems.append(val[1:3])
    elems.append(val[3])
    elems.append(val[4:val.find('/')])
    elems.append(val[val.find('/')-2:val.find('/')+2])
    elems.append(val[val.find('/')-2:val.find('/')+3])
    return elems

并将其应用,然后将该列拆分为多列(*编辑以删除不必要的lambda)

df['p']=df['CPC'].apply(parse_cpc)*

    a   CPC p
0   1   Y10T403/4602    [Y, 10, T, 403, 03/4, 03/46]
1   2   H02S20/00   [H, 02, S, 20, 20/0, 20/00]
2   3   H01L31/02168    [H, 01, L, 31, 31/0, 31/02]

df[[1,2,3,4,5,6]]=pd.DataFrame(df['p'].values.tolist())

    a   CPC             p                               1   2   3   4   5       6
0   1   Y10T403/4602    [Y, 10, T, 403, 03/4, 03/46]    Y   10  T   403 03/4    03/46
1   2   H02S20/00       [H, 02, S, 20, 20/0, 20/00]     H   02  S   20  20/0    20/00
2   3   H01L31/02168    [H, 01, L, 31, 31/0, 31/02]     H   01  L   31  31/0    31/02

然后删除过渡列

df.drop('p', axis=1, inplace=True)

    a   CPC             1   2   3   4   5       6
0   1   Y10T403/4602    Y   10  T   403 03/4    03/46
1   2   H02S20/00       H   02  S   20  20/0    20/00
2   3   H01L31/02168    H   01  L   31  31/0    31/02

答案 1 :(得分:2)

这是在自定义函数中使用ERROR in ./src/app/app.component.ts Module not found: Error: Can't resolve'regex的{​​{1}}模式的另一种潜在方法:

Series.extract