Home >Backend Development >Python Tutorial >How Can I Combine Text Columns in a Pandas DataFrame to Create a New Column?
In pandas, data manipulation often involves combining columns to derive meaningful insights. One such scenario arises when we need to merge text columns to create a new text column.
Consider the following dataframe with two text columns, "Year" and "quarter":
Year quarter 2000 q2 2001 q3
Our objective is to create a new column called "period" that combines the "Year" and "quarter" columns to obtain values like "2000q2" and "2001q3."
To achieve this, we can directly concatenate the "Year" and "quarter" columns if both are strings:
df["period"] = df["Year"].astype(str) + df["quarter"]
However, if one or both columns are not strings, we must convert them to strings before concatenating. For instance:
df["period"] = df["Year"].astype(str) + df["quarter"]
It's essential to be cautious of NaN values when performing this operation, as they may lead to unintended results.
Alternatively, if multiple string columns need to be joined, the agg method can prove useful:
df['period'] = df[['Year', 'quarter', ...]].agg('-'.join, axis=1)
In this case, "-" is used as the separator, but you can customize it as needed.
The above is the detailed content of How Can I Combine Text Columns in a Pandas DataFrame to Create a New Column?. For more information, please follow other related articles on the PHP Chinese website!