Python Pandas: Replace Hyphens (-) with Spaces in Column Names
Yes, you can utilize the str.replace() method to substitute the '-' character with a space in the 'name' column of a pandas DataFrame. Here's a code example:
import pandas as pd
# Create an example DataFrame
df = pd.DataFrame({'name': ['John-Doe', 'Jane-Smith', 'Bob-Johnson']})
# Replace '-' with ' ' in the 'name' column
df['name'] = df['name'].str.replace('-', ' ')
# Display the updated DataFrame
print(df)
Output:
name
0 John Doe
1 Jane Smith
2 Bob Johnson
This code snippet effectively demonstrates how to use str.replace() to enhance the readability of your DataFrame by replacing hyphens with spaces in the 'name' column.
原文地址: https://www.cveoy.top/t/topic/nTJ8 著作权归作者所有。请勿转载和采集!