If I have this table:
| City | State | Person 1 | Person 2 |
|---|---|---|---|
| Atlanta | GA | Bob | Fred |
But, I want to convert it to:
| City | State | Person# | Person Name |
|---|---|---|---|
| Atlanta | GA | 1 | Bob |
| Atlanta | GA | 2 | Fred |
What is the most efficient way to accomplish this?
If I have this table:
| City | State | Person 1 | Person 2 |
|---|---|---|---|
| Atlanta | GA | Bob | Fred |
But, I want to convert it to:
| City | State | Person# | Person Name |
|---|---|---|---|
| Atlanta | GA | 1 | Bob |
| Atlanta | GA | 2 | Fred |
What is the most efficient way to accomplish this?
Use melt:
out = df.melt(['City', 'State'], var_name='Person#', value_name='Person Name')
out['Person#'] = out['Person#'].str.extract('(\d+)')
>>> out
City State Person# Person Name
0 Atlanta GA 1 Bob
1 Atlanta GA 2 Fred