Combining Columns to Create a New Period Column in a Pandas DataFrame
In pandas, you may encounter instances where you need to combine columns to create a new column, often representing a combination of their values. Consider a scenario where you have a DataFrame with columns 'Year' and 'quarter,' as shown below:
Year quarter 2000 q2 2001 q3
You want to create a new column named 'period' that combines the values from the 'Year' and 'quarter' columns, resulting in a DataFrame that looks like this:
Year quarter period 2000 q2 2000q2 2001 q3 2001q3
Combining String Columns
If both 'Year' and 'quarter' are strings, you can concatenate them directly using the ' ' operator:
df["period"] = df["Year"] + df["quarter"]
Combining Non-String Columns
If either 'Year' or 'quarter' is not a string, you need to convert it to a string first, as demonstrated below:
df["period"] = df["Year"].astype(str) + df["quarter"]
NOTE: Be cautious of NaN values when performing this operation.
Combining Multiple String Columns Using agg
If you have multiple string columns to combine, you can use the 'agg' function to apply a join operation:
df['period'] = df[['Year', 'quarter', ...]].agg('-'.join, axis=1)
In this case, '-' is the separator used to concatenate the columns.
By following these steps, you can effectively combine columns in a pandas DataFrame to create a new column that represents a combination of their values.
The above is the detailed content of How Can I Combine Pandas DataFrame Columns to Create a New Period Column?. For more information, please follow other related articles on the PHP Chinese website!