2017-09-07 75 views
1

的合併列我有這樣的數據幀:錯誤當熊貓數據幀

    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 

我在做什麼錯在這裏?字符串訪問的

+0

您的列名中有錯字。兩列名爲'Telefone1'的列,但您正在嘗試添加'Telefone1'和'Telefone2'。當我爲此糾正時,你的代碼工作正常。 – piRSquared

+0

我想你應該使用.map函數而不是astype,請嘗試下面的代碼:dataframe [「Telefone」] = df [「Telefone1」]。map(str)+ df [「Telefone1」]和列名是telefone1 –

+0

實際上,@piRSquared是我在這裏把這個錯誤帶到了stackoverflow。這是我在打字時犯的一個錯誤(已經在這裏修復了)。在我的代碼中,它是正確的,它正在得到這個回溯。 – abutremutante

回答

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 

或:

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

這與您的樣本數據。如果發生錯誤,請創建一個新列以指示每個字段的長度並對此值進行排序。可能會有數據錯誤。

0

使用貓()函數:

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

謝謝,但這帶我到另一個錯誤:AttributeError:'DataFrame'對象沒有屬性'str' – abutremutante

+0

@abutremutante,檢查編輯答案 –

1
df = df.applymap(str) 

選項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 

選項2

df.apply

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