我想辦法合併兩個數據幀和從指定的數據幀保持不相交的數據。
問題:
我有重複的數據和I預期這條線,以去除重複數據:
final_df = new_df[~new_df.isin(previous_df)].dropna()
實施例的數據和數據測試:
record = Record(1000, 9300815, '<redacted type>', '<redacted id>')
test_df = pd.DataFrame([record])
if not final_df.empty:
# this produces an empty data frame
empty_df = test_df[test_df.isin(final_df)].dropna()
# this produces the record
record_df = final_pdf[final_pdf.col01 == record.col01]
背景:
我正在加載xml數據並將xml文件轉換爲幾種不同的記錄rd類型爲namedtuples。我將每種記錄類型分成它自己的數據框。然後,我從XML文件已經通過構建previous_df
這樣加載到數據庫中的數據比較當前的一組數據:
previous_df = pd.read_sql_table(table_name, con=conn, schema=schema, columns=columns)
列是動態創建的基於命名元組的字段。數據庫模式是使用sqlalchemy生成的,並且當我認爲數據庫中存在重複時,我添加了UniqueConstraint
進行管理。
在此先感謝您提供的任何幫助。
看[pandas.DataFrame.duplicated](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.duplicated.html#pandas-dataframe-duplicated )。如果你添加一些樣本數據和預期的輸出,你會得到一些詳細的幫助。 –
我也使用過'drop_duplicates(keep = False)',它有效地調用'duplicateated',然後在一行中刪除它們。我真的需要一種合併兩個數據框並移除它們的交集的好方法。 –
我會推薦查看關於合併數據框的[Pandas Documentation](https://pandas.pydata.org/pandas-docs/stable/merging.html)。我沒有看到你想要去的具體位置,但是你可以在那裏找到解決問題的方法 –