Append a character or numeric to the column in pandas python can be done by using “+” operator. simple “+” operator is used to concatenate or append a character value to the column in pandas. similarly we can also use the same “+” operator to concatenate or append the numeric value to the start or end of the column. Let’s see how to append or add the value to the column in pandas
- Append or add a character or string to start of the column in pandas
- Append or add a character or string to end of the column in pandas
- Append or add a numeric or integer value to the start of the column in pandas
- Append or add a numeric or integer value to the end of the column in pandas
Create dataframe : Append a character or numeric value to the column in pandas python
First let’s create a dataframe
import pandas as pd import numpy as np df1 = { 'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'], 'Score1':[4,47,55,74,31]} df1 = pd.DataFrame(df1,columns=['State','Score1']) print(df1)
df1 will be
Append or add a character or string value to start of the column in pandas:
Appending the character or string to start of the column in pandas is done with “+” operator as shown below.
df1['State_new'] ='USA-' + df1['State'].astype(str) print(df1)
So the resultant dataframe will be
Append a character or string value to end of the column in pandas:
Appending the character or string to end of the column in pandas is done with “+” operator as shown below.
df1['State_new'] = df1['State'].astype(str) + '-USA' print(df1)
So the resultant dataframe will be
Append or concatenate a numeric value to start of the column in pandas:
Appending the numeric value to start of the column in pandas is done with “+” operator as shown below.
df1['State_new'] ='101' + df1['State'].astype(str) print(df1)
So the resultant dataframe will be
Append or concatenate a numeric value to end of the column in pandas:
Appending the numeric value to end of the column in pandas is done with “+” operator as shown below.
df1['State_new'] = df1['State'].astype(str) + '101' print(df1)
So the resultant dataframe will be
Other Related Topics:
for difference between (+) and append() function refer here