1

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)

perl
  • 9,826
  • 1
  • 10
  • 22
Vni Versvs
  • 21
  • 5
  • Does this answer your question? [String concatenation of two pandas columns](https://stackoverflow.com/questions/11858472/string-concatenation-of-two-pandas-columns) – perigon Mar 09 '21 at 22:19
  • 1
    For example with `df.apply(', '.join, axis=1)` – perl Mar 09 '21 at 22:22
  • That format is _okay_, someone can quickly get your data from it with `pd.read_clipboard`. The how-to-ask guide is site-wide, but there's a pretty detailed post about `pandas` specific tips for reproducible data: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples with a few better ways – ALollz Mar 09 '21 at 22:35
  • whoever edited my question has misrepresented it. I said I want all the info in one cell THAT TAKES UP 4 columns – Vni Versvs Mar 09 '21 at 22:40
  • 1
    @VniVersvs that's not possible. There's no such thing as a "merged cell" in pandas (in the way that there is in excel). – ALollz Mar 09 '21 at 22:42
  • so i made a small picture with a "what i have" and then "what i want" https://ibb.co/zfSZwPK – Vni Versvs Mar 09 '21 at 22:46
  • @ALollz i see. Perhaps some other library has it? – Vni Versvs Mar 09 '21 at 22:48

2 Answers2

0

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 (e.g. use join clause in the assignment):

df['name'] = df.apply(', '.join, axis=1)
df['phone'] = df['address'] = df['neighborhood'] = ''
Gal R
  • 21
  • 5
  • I'm sorry, but whoever edited my question misrepresented it, and i have no idea how to revert to the original editing. The question as it stands above is not what I need answered. – Vni Versvs Mar 10 '21 at 16:58
0

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
Pablo C
  • 4,661
  • 2
  • 8
  • 24
  • I'm sorry, but whoever edited my question misrepresented it, and i have no idea how to revert to the original editing. The question as it stands above is not what I need answered. – Vni Versvs Mar 10 '21 at 16:58