So I have a dataframe
name | phone | address | neighborhood |
---|---|---|---|
client1 | xxxxx | AStreet | Brooklyn ------ |
client2 | xxxxx | BStreet | Brooklyn ------ |
etc...
I need to "combine" the cells in each row to be a single cell with all the info, occupying 4 columns. It should look like this:
name | phone | address | neighborhood |
---|---|---|---|
client1, xxxxx, AStreet, Brooklyn -------- | |||
client2, xxxxx, BStreet, Brooklyn -------- |
How to do this? Is this even posible? Sorry for the bad formatting, the guide wasn't very helpful on how to present a proper dataframe.
I've looked into many pandas methods for dataframes, but couldn't find an answer (perhaps i didn't notice it could be a solution, since i'm a beginner programmer)
There's no explicit operation for this in pandas.
Yet, a straight forward solution would be something like this:
df['name'] = df['name'] + ', ' + df['phone'] + ', ' + df['address'] + ', ' + df['phone']
df['phone'] = df['address'] = df['neighborhood'] = ''
You can always improve this (eg use join
clause in the assignment):
df['name'] = df.apply(', '.join, axis=1)
df['phone'] = df['address'] = df['neighborhood'] = ''
IIUC this is what you want:
out = pd.DataFrame(columns = df.columns)
df.iloc[:,:-1] += ', '
out["name"] = df.sum(1)
Output
name phone address neighborhood
0 client1, xxxxx, AStreet, Brooklyn ------ NaN NaN NaN
1 client2, xxxxx, BStreet, Brooklyn ------ NaN NaN NaN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.