2017-06-11 8 views
1

を作成するとき、私にはないのxlsxファイルから呼び出す:私は同じ情報で自分自身をデータフレームを作成するとき、私は2つのデータフレームでこれを行うにしようとしています、私は属性のエラーを取得するが、私はデータフレームパンダ

df1 = df.copy() 

df1['emails'] = df1.emails.apply(lambda x: ','.join(set(map(str.strip, x.split(','))) - set(blacklisted.email))) 

df1 = df1[df1.emails != ''] 

それは同じデータ型を返します。

blacklisted=pd.DataFrame(columns=['email'], 
       data=[['[email protected]'],['[email protected]'],['[email protected]'], ['[email protected]'], 
         ['[email protected]']]) 

blacklisted.head() 

email 
0 [email protected] 
1 [email protected] 
2 [email protected] 
3 [email protected] 
4 [email protected] 

とこのようになり、他のデータフレーム:例えば私はこのようになりますデータフレーム作成する場合

df=pd.DataFrame(columns=['customerId','full name','emails'], 
       data=[['208863338', 'Brit Spear', '[email protected]'],['086423367', 'Justin Bob', '[email protected],[email protected]'],['902626998', 'White Ice', '[email protected],[email protected]'], ['1000826799', 'Bear Lou', '[email protected]'], 
         ['1609813339', 'Ariel Do', '[email protected], [email protected]']]) 
print(df) 



customerId  full name  emails 
0 208863338 Brit Spear [email protected] 
1 086423367 Justin Bob [email protected],[email protected] 
2 902626998 White Ice [email protected],[email protected] 
3 1000826799 Bear Lou [email protected] 
4 1609813339 Ariel Do [email protected], [email protected] 

上記のコードの作品を​​、私は二つのファイルから同じ情報を呼び出すしようとすると、このような代わりに使用してコード:私はそれの上に文句を言わない仕事を作成した2つのデータフレームとまったく同じ情報を持つ

blacklisted = df1 = pd.read_excel(r'C:/Users/Administrator/Documents/sfiq/blacklisted.xlsx') 
df = pd.read_excel(r'C:/Users/Administrator/Documents/customers.xlsx') 

、私は属性のエラーが表示されます。

エラーが返さ
df1['emails'] = df1.emails.apply(lambda x: ','.join(set(map(str.strip, x.split(','))) - set(blacklisted.email))) 

です:

--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-22-439d1f152f33> in <module>() 
----> 1 df1['emails'] = df1.emails.apply(lambda x: ','.join(set(map(str.strip, x.split(','))) - set(blacklisted.email))) 

C:\Program Files\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds) 
    2218   else: 
    2219    values = self.asobject 
-> 2220    mapped = lib.map_infer(values, f, convert=convert_dtype) 
    2221 
    2222   if len(mapped) and isinstance(mapped[0], Series): 

pandas\src\inference.pyx in pandas.lib.map_infer (pandas\lib.c:62658)() 

<ipython-input-22-439d1f152f33> in <lambda>(x) 
----> 1 df1['emails'] = df1.emails.apply(lambda x: ','.join(set(map(str.strip, x.split(','))) - set(blacklisted.email))) 

AttributeError: 'float' object has no attribute 'split' 
+0

あなたはあなたの関数を適用する前に、 'プリント(df.emails)'権利を試すことはできますか? 1つの理由は、あなたのExcelから出てくるあなたの 'df'に予期しない要素があるかもしれません。チェックする価値がある。 –

答えて

1

は、あなたが持っていると仮定します。blacklisted.xlsx

customers.xlsx

enter image description here

enter image description here

使用astype前に、このような機能を適用します。

blacklisted = pd.read_excel(r'blacklisted.xlsx') 
df = pd.read_excel(r'customers.xlsx') 
df['emails'] = df.emails.astype(str).apply(lambda x: ','.join(set(map(str.strip, x.split(','))) - set(blacklisted.email))) 
df 

df次のようになります。

customerId full name emails 
0 208863338 Brit Spear [email protected] 
1 86423367 Justin Bob [email protected],[email protected] 
2 902626998 White Ice [email protected],[email protected] 
3 1000826799 Bear Lou [email protected] 
4 1609813339 Ariel Do [email protected],[email protected] 
関連する問題