1

I want to populate a column with another field and text. You can calculate a column with another column like this:

DF["column_name1"] = DF["column_name2"]

And you can calculate it with text like this

DF["column_name1"] = "TEXT"

How do I combine the two? For example I want to get:

column_name1
| column_name1 | column_name2 |
| ------------ | ------------ |
| 1            | MN-1         |
| 2            | MN-2         |
| 3            | MN-3         |

How do I get every column to start with "MN" and then the number from column_name1? I could create a new column, populate it with "MN" and then delete it later but that seems like extra steps.

Thanks for your help.

Thanks for your help! Thanks for your help!

1 Answers1

0

You can do it the same way you do with calculating other columns, just make sure to convert the other column's value to a string first:

# Create simple dataframe
DF = pd.DataFrame([{"column_name1":1}, {"column_name1":2}, {"column_name1":3}])

# Calculate string column
DF["column_name2"] = "MN-" + DF["column_name1"].astype(str)

print(DF)

This yields:

   column_name1 column_name2
0             1         MN-1
1             2         MN-2
2             3         MN-3
Caleb Keller
  • 553
  • 3
  • 19