How to get extract first 3 letters from a string in dataframe column and store them in a new column.
Extract the first three letters/numbers from the column 'A' and store it in the column 'C'
DATA:
A B
12525 1FWE23
14654 22354
24798 32684
38945 45368
46456 46485
AD545 45346
A5D66 58446
EXPECTED :
A B C
12525 1FWE23 125
14654 22354 146
24798 32684 247
38945 45368 389
46456 46485 464
AD545 45346 AD5
A5D66 58446 A5D
CodePudding user response:
First, we re-create the dataframe from the example you provided:
from io import StringIO
s = """
A B
12525 1FWE23
14654 22354
24798 32684
38945 45368
46456 46485
AD545 45346
A5D66 58446
"""
df = pd.read_csv(StringIO(s), sep="\s ", engine='python')
Next, create a new column C
by using string accessor methods:
df['C'] = df['A'].str.slice(0, 3)
# Equivalently:
# df['C'] = df['A'].str[:3]
Verifying that the result is as desired via print(df)
:
A B C
0 12525 1FWE23 125
1 14654 22354 146
2 24798 32684 247
3 38945 45368 389
4 46456 46485 464
5 AD545 45346 AD5
6 A5D66 58446 A5D