2

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?

1
  • Hello, do you want to split by person *? Commented Jul 27, 2021 at 15:19

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.