的合併列我有這樣的數據幀:錯誤當熊貓數據幀
Telefone1 Telefone2
CNPJ
44167450000149 1332385314 1332385314
56095862000108 2125439090 2125439090
59664391000191 1143990005 1143990005
我想「Telefone1」和「Telefone2」合併成一個單一的列。它應該是這樣的:
Telefone
CNPJ
44167450000149 1332385314,1332385314
56095862000108 2125439090,2125439090
59664391000191 1143990005,1143990005
對於我使用這個:
df['Telefone']=df.Telefone1.astype(str)+","+df.Telefone2.astype(str)
而且我得到這個回溯:
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
我在做什麼錯在這裏?字符串訪問的
您的列名中有錯字。兩列名爲'Telefone1'的列,但您正在嘗試添加'Telefone1'和'Telefone2'。當我爲此糾正時,你的代碼工作正常。 – piRSquared
我想你應該使用.map函數而不是astype,請嘗試下面的代碼:dataframe [「Telefone」] = df [「Telefone1」]。map(str)+ df [「Telefone1」]和列名是telefone1 –
實際上,@piRSquared是我在這裏把這個錯誤帶到了stackoverflow。這是我在打字時犯的一個錯誤(已經在這裏修復了)。在我的代碼中,它是正確的,它正在得到這個回溯。 – abutremutante