E
E
Eugene Kuznetsov2021-09-08 05:38:16
Python
Eugene Kuznetsov, 2021-09-08 05:38:16

How to split a text entry in a pandas dataframe into multiple entries (lines) by delimiter?

Good afternoon.
I have a notepad with the following:

import pandas as pd
df = pd.read_csv("yyy.csv")
df.shape
>>(52350, 14)

One of the "PP" columns with strings (comma-separated integers), let the rest be the "C" column to illustrate the behavior.
In the table, the data looks like this:
PP | C
---------+-------
1, 2 | T
5 | C
18, 20 | S
7 | T
We must do this:
PP | C
---------+-------
1 | T
2 | T
5 | C
18 | S
20 | S
7 | T
Elements in the PP column can be left as text, they will then be grouped by them. The number of elements is from 1 (85% of records) to 3 (2-3% of records). When an element is moved to a new record, the information on the remaining columns is simply duplicated.
Is it possible to somehow contrive to do this without "manual" iteration in a loop through all the records, given the number of records?
There are a lot of examples on the Internet on splitting into columns. Unfortunately, I did not find the "by lines" option.
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question