'Inserting a column in a pandas data frame

I'm working with a data frame with postal codes, and I'm trying to add to the data frame with the postal codes 2 columns: one with the full postal code and other with the first 4 numbers. I have already come up with the code above and it works:

 df=pd.DataFrame({"Codigo_Postal":['4430-383',
'4430-383',
'4430-362',
'4430-383',
'4430-383',
'4430-383',
'4430-812',
'4430-812',
'4430-812',
'4415-364',
'4415-226',
'4415-226',
'4415-350']})


list_cp4=[]
list_cp7=[]
for i in range(len(df)):
    postal_code=df.loc[i,'Codigo_Postal',]
    list_cp4.append(postal_code[0:4])
    list_cp7.append(postal_code[0:8])

df['CP4']=list_cp4
df['CP7']=list_cp7

I was only wondering if there is a more efficient way of doing this, since I have a very large data frame (more than 500000 entries).

Thank you anyway!



Solution 1:[1]

You could use str accessor:

df['CP4'] = df['Codigo_Postal'].str[:4]
df['CP7'] = df['Codigo_Postal'].str[:8]

Output:

   Codigo_Postal   CP4       CP7
0       4430-383  4430  4430-383
1       4430-383  4430  4430-383
2       4430-362  4430  4430-362
3       4430-383  4430  4430-383
4       4430-383  4430  4430-383
5       4430-383  4430  4430-383
6       4430-812  4430  4430-812
7       4430-812  4430  4430-812
8       4430-812  4430  4430-812
9       4415-364  4415  4415-364
10      4415-226  4415  4415-226
11      4415-226  4415  4415-226
12      4415-350  4415  4415-350

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1