Reputation: 11
I have a list of emails, phones and user info that I want to output in csv but I need to follow a format that contains duplicate columns.
email, email, phone, phone, phone, name, address
[email protected], [email protected], 90192, 2980, 9203, John Doe, 82 High Street
[email protected], [email protected], 1341, 55, 665, Roe Jan, 11 Low Street
[email protected],,, 55, 111, Roe Jan, 11 Low Street
Is this possible in pandas? What is the best way of adding rows and columns with same name?
Upvotes: 1
Views: 3409
Reputation: 1
Use pd.conact to add columns with the same name:
df = pd.concat([df1, df2], axis=1)
It will concatenate all columns, even if some of them have the same name.
Upvotes: 0
Reputation: 4767
Build your dataframe with different column names (email01, email02...) and then use a header list on output:
df.to_csv("file.csv",header=['email', 'email', 'phone', 'phone', 'phone', 'name', 'address'])
Upvotes: 1
Reputation: 16792
You could get it done using csv
:
list.txt:
email, email, phone, phone, phone, name, address
[email protected], [email protected], 90192, 2980, 9203, John Doe, 82 High Street
[email protected], [email protected], 1341, 55, 665, Roe Jan, 11 Low Street
[email protected],,, 55, 111, Roe Jan, 11 Low Street
and then:
import csv
with open('list.txt', 'r') as readFile:
reader = csv.reader(readFile)
lines = list(reader)
with open('people.csv', 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)
readFile.close()
writeFile.close()
OUTPUT (people.csv):
Upvotes: 1