How to add a string to each value in a pandas column?

To add a string to each value of a pandas column, use the pandas.Series.str.cat() method along with the desired string. This method will concatenate the values of the column and the given string and return the resulting series. The code would look something like this: df[‘column_name’] = df[‘column_name’].str.cat(string)

This method can be used to add a prefix or a suffix to each value in the column. It can also be used to add a space between each value and the string. Additionally, the str.cat() method can be used to add multiple strings to each value in the column at once.


You can use the following methods to add a string to each value in a column of a pandas DataFrame:

Method 1: Add String to Each Value in Column

df['my_column'] = 'some_string' + df['my_column'].astype(str)

Method 2: Add String to Each Value in Column Based on Condition

#define condition
mask = (df['my_column'] == 'A')

#add string to values in column equal to 'A'
df.loc[mask, 'my_column'] = 'some_string' + df['my_column'].astype(str)

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    A      22        7         8
2    A      19        7        10
3    A      14        9         6
4    B      14       12         6
5    B      11        9         5
6    B      20        9         9
7    B      28        4        12

Example 1: Add String to Each Value in Column

The following code shows how to add the string ‘team_’ to each value in the team column:

#add string 'team_' to each value in team column
df['team'] = 'team_' + df['team'].astype(str)

#view updated DataFrame
print(df)

     team  points  assists  rebounds
0  team_A      18        5        11
1  team_B      22        7         8
2  team_C      19        7        10
3  team_D      14        9         6
4  team_E      14       12         6
5  team_F      11        9         5
6  team_G      20        9         9
7  team_H      28        4        12

Notice that the prefix ‘team_’ has been added to each value in the team column.

You can also use the following syntax to instead add ‘_team’ as a suffix to each value in the team column:

#add suffix 'team_' to each value in team column
df['team'] = df['team'].astype(str) + '_team'

#view updated DataFrame
print(df)

     team  points  assists  rebounds
0  A_team      18        5        11
1  A_team      22        7         8
2  A_team      19        7        10
3  A_team      14        9         6
4  B_team      14       12         6
5  B_team      11        9         5
6  B_team      20        9         9
7  B_team      28        4        12

Example 2: Add String to Each Value in Column Based on Condition

The following code shows how to add the prefix ‘team_’ to each value in the team column where the value is equal to ‘A’:

#define condition
mask = (df['team'] == 'A')

#add string 'team_' to values that meet the condition
df.loc[mask, 'team'] = 'team_' + df['team'].astype(str)

#view updated DataFrame
print(df)

     team  points  assists  rebounds
0  team_A      18        5        11
1  team_A      22        7         8
2  team_A      19        7        10
3  team_A      14        9         6
4       B      14       12         6
5       B      11        9         5
6       B      20        9         9
7       B      28        4        12

Notice that the prefix ‘team_’ has only been added to the values in the team column whose value was equal to ‘A’.

x