How to Split a Dataframe String Column into Two Columns
In this scenario, the dataframe df contains a single string column named 'row' and the goal is to split it into two new string columns: 'fips' and 'row'.
To accomplish this, the .str.split() method can be employed. This method enables splitting a string on a specified delimiter (in this case, a known separator like a space) and returning a Series of lists.
df[['fips', 'row']] = df['row'].str.split(' ', n=1, expand=True)
Here's a breakdown of how each parameter in the above code functions:
The result is a dataframe with three columns: 'fips', 'row', and the original 'row' column still intact.
This method effectively splits the 'row' column based on the space delimiter and creates two additional columns, 'fips' and 'row', containing the respective parts of the split string.
The above is the detailed content of How to Split a Pandas DataFrame String Column into Two Using `.str.split()`?. For more information, please follow other related articles on the PHP Chinese website!