Sungjin
Sungjin

Reputation: 67

python - is it possible to extract and resize data in data frame?

I would like to extract the data from data frame, for example, I have two columns which are All_id and Patnum

                                           All_id      PatNum
    429/326 | 429/218.1 | 429/219 | 429/220 | 429/221   US4057679
                      429/185 | 429/215 | 429/231.95    US4071662
                                  429/623.5 | 429/221   US4021911
                                             429/136    US4041218
                         429/171 | 429/174 | 429/180    US4048401

the expected result that I would like to be is

All_id     PatNum
429/326    US4057679
429/218.1  US4057679
429/219    US4057679
429/220    US4057679
429/221    US4057679
429/185    US4071662
429/215    US4071662
429/231.95 US4071662
429/623.5  US4021911
429/221    US4021911
429/136    US4041218
429/171    US4048401
429/174    US4048401
429/180    US4048401

thank you in advance

Upvotes: 1

Views: 158

Answers (1)

BENY
BENY

Reputation: 323316

Flatten your string to list and using split with repeat recreate your dataframe

s=df.All_id.str.split('|')
pd.DataFrame({'All_id':sum(s,[]),'PatNum':df.PatNum.repeat(s.str.len())})
Out[484]: 
      All_id     PatNum
0    429/326  US4057679
0  429/218.1  US4057679
0    429/219  US4057679
0    429/220  US4057679
0    429/221  US4057679

Upvotes: 1

Related Questions