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.
#1
I have csv file with fake data. This is part:
Quote:Name1,Telephone1,Name2,Telephone2,Duration
Alice,085 646 4923,Shawn,085 388 3678,7
Derek,082 467 5217,Justin,082 245 8825,9
Derek,082 467 5217,Kim,083 278 8489,48
Benjamin,084 614 6635,Henry,084 233 5472,15
David,084 852 1646,Helen,085 962 5673,17
Joseph,085 655 8640,Elva,082 984 4322,44
Alvin,083 268 1145,Cheryl,085 140 6517,2
Henry,084 233 5472,Justin,082 245 8825,40
Karla,083 936 4576,Ann,083 403 7003,35
Henry,084 233 5472,Nicole,085 448 3607,39
Steven,082 792 4993,John,084 340 3759,13
Kenya,083 464 8078,Kim,083 278 8489,16
Alfred,085 749 1638,Samuel,084 603 8830,48
Mac,083 176 7486,Phyllis,084 638 7641,11
Donna,085 532 8752,Brandon,085 402 7641,23
Alvin,083 268 1145,Jennette,083 227 5242,14
I want to create two columns, the first one with all name and the second one with all telephone numbers.
I thought about something like that:
import pandas as pd
import numpy as np
dane=pd.read_csv('telephone_calls.csv')
names=pd.DataFrame(dane,columns=['Name1','Name2'])
print(names.unstack())
numbers=pd.DataFrame(dane,columns=['Telephone1','Telephone2'])
print(numbers.unstack())
But it does work as it should.
Reply
#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
#3
#!/usr/bin/python3
import pandas as pd
import numpy as np

dane=pd.read_csv('calls.csv')

names1 = pd.DataFrame(dane,columns=['Name1', 'Telephone1'])
names1 = names1.rename({'Name1': 'Name', 'Telephone1': 'Tel'}, axis='columns')

names2 = pd.DataFrame(dane,columns=['Name2', 'Telephone2'])
names2 = names2.rename({'Name2': 'Name', 'Telephone2': 'Tel'}, axis='columns')

allnames = pd.concat([names1, names2], ignore_index=True)
print(allnames)
Reply


Forum Jump:

User Panel Messages

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