2017-03-24 3 views
1

私のCSVには2つの列があります(新しい家の住所とcsvの中でCanがキャンセルされました)。アドレスが取り消された場合、Canの下にTrueを書き込む必要がありますが、エンドユーザーがTrueを書き込むのを忘れて、同じAddressが2回表示されることがあります。私はPythonにを教えてください。(削除しないでください)最初に取り消されることなく2回現れるアドレス。重複した住所を表示するパンダ

例:あなたは上記の例から見ることができたよう

Date_Booked   Address of New Home      Can 

01/07/2017   1234 SO Drive        True 
02/14/2017   4321 Python Court 
03/17/2017   1234 SO Drive 
03/23/2017   4321 Python Court  

、1234 SOドライブがキャンセルされたと真が書かれていた、それは二度書かれた理由をされて、これは私たちが望むものであるが、4321 Pythonの裁判所はそれを取り消されましたCanceledの下ではTrueとは言わないので、CSVに2回表示され、あらゆる種類の問題が発生します。どのような援助がいただければ幸いです

Traceback (most recent call last): 
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279) 
File "pandas\src\hashtable_class_helper.pxi", line 404, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:8543) 
TypeError: an integer is required 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 

dup_addresses = non_cancelled.groupby('Address of New Home').filter(lambda x: len (x) > 1) 
KeyError: 'Address of New Home' 

import pandas as pd 

first = pd.read_csv('Z:PCR.csv') 
df = pd.DataFrame(first) 

non_cancelled = df['Can'].apply(lambda x: x != 'True') 

dup_addresses = non_cancelled.groupby('Address of New Home').filter(lambda x: len (x) > 1) 
if not dup_addresses.empty: 
    raise Exception ('Same address written twice without cancellation') 

私は、次のエラーを取得しています。

答えて

1

これがすでにあるTrueを維持してCan列を更新する必要があるとして、Trueを返しますDATAFRAMEのすべての行を与えます見逃したもので更新する。

can = df.duplicated(subset=['Address of New Home'], keep='last') 
df['Can'] = df.Can.combine_first(can.where(can, '')) 
print(df) 

    Date_Booked Address of New Home Can 
0 01/07/2017  1234 SO Drive True 
1 02/14/2017 4321 Python Court True 
2 03/17/2017  1234 SO Drive  
3 03/23/2017 4321 Python Court  

要求パー

can = df.duplicated(subset=['Address of New Home'], keep='last') 
df['Can'] = df.Can.combine_first(pd.Series(np.where(can, 'Missed', ''), df.index)) 
print(df) 

    Date_Booked Address of New Home  Can 
0 01/07/2017  1234 SO Drive True 
1 02/14/2017 4321 Python Court Missed 
2 03/17/2017  1234 SO Drive   
3 03/23/2017 4321 Python Court 
+0

ありがとう、とにかく「True」でそれらを更新するのではなく、「End User Missed」でそれらを更新できます。 –

+0

npは 'numpy'だと思います。 –

+0

@JakeWagnerはい、申し訳ありません。この場合はどこにいても便利です。構文はより洗練されています。私たちはまだパンダを使うことができましたが、それはもっと醜いです。これを解決するには、さまざまな方法が考えられます。最も挑戦していたことを理解していました。 – piRSquared

0

あなたの列はAddress_of_New_Homeであり、Address of New Homeではありません。 [「に」]

non_cancelled = df['Can'].apply(lambda x: x != 'True') 

この引数を適用すると、あなたは、シリーズのDFにまで適用されるので、この方法では、シリーズではないが返されます。ただ、問題はこの文であるアンダースコアに

+0

申し訳ありませんが、プログラミングに使用するので、私の謝罪、アンダースコアでそれを書きました。 「新しい家の住所」でなければなりません。 –

0

を忘れてしまいました完全なDataFrame説明するために、ここでいくつかのコードは次のとおりです。

import pandas as pd 
import numpy as np 

df = pd.DataFrame({'a': np.arange(0,5), 'b': np.arange(5,10), 'c': np.arange(10,15)}) 
print(df) 

出力はこの

a b c 
0 0 5 10 
1 1 6 11 
2 2 7 12 
3 3 8 13 
4 4 9 14 

あるしかし、私はこれを行うとき:

a = df['a'].apply(lambda x: x*20) 
print(a) 

を私が取得:

0  0 
1 20 
2 40 
3 60 
4 80 

へ何をしたいのですか?代わりにこれをやってみてください:

non_cancelled = df[df['Can'] != True] 

これは私たちに条件(DFの[「に」]!=真)は