1

I have this dataframe:

                 Telefone1   Telefone2
CNPJ                                  
44167450000149  1332385314  1332385314
56095862000108  2125439090  2125439090
59664391000191  1143990005  1143990005

I want to merge "Telefone1" and "Telefone2" into one single column. It should look like this:

                Telefone
CNPJ                                  
44167450000149  1332385314,1332385314
56095862000108  2125439090,2125439090
59664391000191  1143990005,1143990005

For that I'm using this:

df['Telefone']=df.Telefone1.astype(str)+","+df.Telefone2.astype(str)

And I'm getting this traceback:

Traceback (most recent call last):
File "/file.py", line 507, in <module>
'file')
File "file.py", line 347, in function
df['Telefone']=df.Telefone1.astype(str)+","+df.Telefone2.astype(str)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2357, in __setitem__
self._set_item(key, value)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2424, in _set_item
NDFrame._set_item(self, key, value)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/generic.py", line 1464, in _set_item
self._data.set(key, value)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 3418, in set
self.insert(len(self.items), item, value)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 3519, in insert
placement=slice(loc, loc + 1))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 2518, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 1663, in __init__
placement=placement, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 90, in __init__
len(self.mgr_locs)))
ValueError: Wrong number of items passed 4, placement implies 1

What am I doing wrong here?

3
  • You have a typo in your column names. Two columns with name 'Telefone1' but you are trying to add 'Telefone1' and 'Telefone2'. When I corrected for this, your code worked fine. Commented Sep 7, 2017 at 23:32
  • I think you should use .map function instead of astype, please try this code:dataframe["Telefone"] = df["Telefone1"].map(str) + df["Telefone1"] and your column names are telefone1 Commented Sep 7, 2017 at 23:33
  • @piRSquared actually I brought that typo up here on stackoverflow. It was a mistake I've made when typing here (already fixed it here). In my code it was correct and it was getting that traceback. Commented Sep 8, 2017 at 23:14

3 Answers 3

1
>>> (df.iloc[:, 0].astype(str) + ',' + df.iloc[:, 1].astype(str)).to_frame('Telephone')
                             Telefone
CNPJ                                 
44167450000149  1332385314,1332385314
56095862000108  2125439090,2125439090
59664391000191  1143990005,1143990005

or:

(df.loc[:, 'Telefone1'].astype(str) + ',' + df.loc[:, 'Telefone2'].astype(str)).to_frame('Telefone'))

This works with your sample data. If there is an error, create a new column to indicate the length of each field and sort on this value. There will likely be data errors.

Sign up to request clarification or add additional context in comments.

Comments

1
df = df.applymap(str)

Option 1

str.cat

df = pd.DataFrame({'Telefone' : df.Telefone1.str.cat(df.Telefone2, sep=',')}, index=df.index)
df
                             Telefone
CNPJ                                 
44167450000149  1332385314,1332385314
56095862000108  2125439090,2125439090
59664391000191  1143990005,1143990005

Option 2

df.apply

df = df.apply(','.join, 1).to_frame(name='Telefone')
df 
                             Telefone
CNPJ                                 
44167450000149  1332385314,1332385314
56095862000108  2125439090,2125439090
59664391000191  1143990005,1143990005

Comments

0

Use cat() function of the string accessor:

df = df.astype(str)
df['Telefone'] = df['Telefone1'].str.cat(df['Telefone2'])

1 Comment

Thanks but this takes me to another error: AttributeError: 'DataFrame' object has no attribute 'str'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.