Home > Article > Backend Development > How do you split text in a column into multiple rows in a pandas DataFrame?
In a large csv file, users often encounter the need to split textual data in a column into separate rows for easier analysis and data manipulation. When working with pandas or Python, several methods can be employed to achieve this objective.
One common approach involves leveraging the split method on the desired column to subdivide the text based on a specified delimiter. For example, to split a column named "Seatblocks" by spaces and colons, the following syntax can be used:
s = df['Seatblocks'].str.split(' ').apply(Series, 1).stack() s.index = s.index.droplevel(-1) # align with df's index s.name = 'Seatblocks' # assign a name for joining
After splitting the column, it can be rejoined with the original DataFrame using the join method:
del df['Seatblocks'] df.join(s)
Alternatively, to create separate columns for each colon-separated string, the following code can be applied:
df.join(s.apply(lambda x: Series(x.split(':'))))
By employing these methods, users can effectively split textual data into multiple rows, allowing for more granular analysis and data manipulation in their programming endeavors.
The above is the detailed content of How do you split text in a column into multiple rows in a pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!