pandas column string first n characters 1

pandas column string first n characters

df1['StateInitial'] = df1['State'].str[:2]
print(df1)  

Here is what the above code is Doing:
1. We are creating a new column called ‘StateInitial’
2. We are using the str method to get the first two characters of the ‘State’ column
3. We are assigning the first two characters of the ‘State’ column to the ‘StateInitial’ column

Similar Posts