Python Forum
The combination of four columns in two.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The combination of four columns in two.
#2
Possibly it would be good idea to remove duplicates (where the same name has the same number)

import pandas as pd
#import numpy as np

df = pd.read_csv('telephone_calls.csv')

names = list(df[['Name1','Name2']].unstack())
numbers = list(df[['Telephone1','Telephone2']].unstack())
# alternative: df['col_1'].append(df['col_2'], ignore_index=True)

for name, num in zip(names, numbers):
    print(name, num)
Output:
Alice 085 646 4923 Derek 082 467 5217 Derek 082 467 5217 Benjamin 084 614 6635 David 084 852 1646 ...
Edit:
If you'd like to keep working on dataframe you could use something like:
import pandas as pd
#import numpy as np

df = pd.read_csv('telephone_calls.csv')

names = df['Name1'].append(df['Name2'], ignore_index=True)
numbers = df['Telephone1'].append(df['Telephone2'], ignore_index=True)

new_df = pd.DataFrame({'Names' : names, 'Numbers': numbers})
print(new_df)

# alternatively:
# new_df = pd.concat([names, numbers], axis=1, keys=['Names', 'Numbers'])
Output:
Names Numbers 0 Alice 085 646 4923 1 Derek 082 467 5217 2 Derek 082 467 5217 3 Benjamin 084 614 6635 4 David 084 852 1646 ...
Reply


Messages In This Thread
RE: The combination of four columns in two. - by michalmonday - May-31-2019, 01:51 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020