Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Python Pandas MultiIndexing: Duplicate rows and add differentiating information in new column

I'm a python beginner trying to duplicate existing rows, while adding differentiating information in a new column. Currently, my DataFrame looks like so:

Patient  Visit
      1     V1
      1     V2
      1     V3
      2     V1
      2     V2

I'd like to add a new column Test which for V1 requires Test 1, but for V2 and V3 requires both Test 1 and Test 2:

Patient  Visit    Test
      1     V1  Test 1
      1     V2  Test 1
      1     V2  Test 2
      1     V3  Test 1
      1     V3  Test 2
      2     V1  Test 1
      2     V2  Test 1
      2     V2  Test 2

I'd then further like to add a column Sample which adds an A and B sample for each test:

Patient  Visit    Test  Sample
      1     V1  Test 1       A
      1     V1  Test 1       B
      1     V2  Test 1       A
      1     V2  Test 1       B
      1     V2  Test 2       A
      1     V2  Test 2       B
...
      2     V2  Test 2       A
      2     V2  Test 2       B

How do I duplicate the rows while adding new information in the additional columns? Thank you for your help!!

question from:https://stackoverflow.com/questions/65644664/python-pandas-multiindexing-duplicate-rows-and-add-differentiating-information

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can manually create your Visits-Test-Sample dataframe, then merge with Patients dataframe:

pd.MultiIndex.from_product([['V2','V3'],['Test 1', 'Test 2'],['A', 'B']], names=['Visit', 'Test', 'Sample'])
  .union(pd.MultiIndex.from_product([['V1'],['Test 1'],['A','B']], names=['Visit', 'Test', 'Sample']))
  .to_frame().reset_index(drop=True)
  .merge(df, on='Visit')
  .sort_values('Patient')

Output:

   Visit    Test Sample  Patient
0     V1  Test 1      A        1
2     V1  Test 1      B        1
4     V2  Test 1      A        1
6     V2  Test 1      B        1
8     V2  Test 2      A        1
10    V2  Test 2      B        1
12    V3  Test 1      A        1
13    V3  Test 1      B        1
14    V3  Test 2      A        1
15    V3  Test 2      B        1
1     V1  Test 1      A        2
3     V1  Test 1      B        2
5     V2  Test 1      A        2
7     V2  Test 1      B        2
9     V2  Test 2      A        2
11    V2  Test 2      B        2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...