2017-02-09 34 views
1

我有一個sqlite數據庫10000 rows,6 columns和1個獨特的列名爲Code。我也有一個pandas df其中有6 columns,一個獨特的列名爲Code9000 rows如何檢查我的sqlite列是否不在我的數據框中?

如何檢查這是1000行是從sql databasedf

我想:

d = connection.execute('SELECT DISTINCT "Code" FROM "my_table"').fetchall() 
for each_row_sql, each_row_df in zip(d, df['Code']): 
    if each_row_sql[0] not in each_row_df: 
     print(each_row_sql[0]) # just to see which are the rows that are not in the sql database 

但是,這是行不通的。它只是返回我的一切

+0

可以使用EXISTS作爲一個布爾 – JB1

回答

1

這裏是一個小的演示:

讓我們首先生成一個樣本DF(10行,3列),並將其寫入到SQLiteDB文件:

In [40]: import sqlite3 
    ...: from sqlalchemy import create_engine 
    ...: 
    ...: engine = create_engine('sqlite:///d:/temp/sqlalchemy_example.db') 
    ...: 
    ...: x = pd.DataFrame(np.random.randint(0, 10, (10,3)), columns=list("abc")) 
    ...: x.insert(0, 'Code', np.arange(len(x))) 
    ...: x.to_sql('my_table', engine, index=False) 
    ...: 

In [41]: x 
Out[41]: 
    Code a b c 
0  0 4 6 6 
1  1 2 5 8 
2  2 3 9 2 
3  3 3 1 2 
4  4 9 8 4 
5  5 2 8 1 
6  6 5 1 8 
7  7 8 9 7 
8  8 0 7 3 
9  9 2 6 3 

現在,讓我們產生了df有5行,3列:

In [42]: df = pd.DataFrame(np.random.randint(0, 10, (5,3)), columns=list("abc")) 
    ...: df.insert(0, 'Code', np.arange(len(df))) 
    ...: 

In [43]: df 
Out[43]: 
    Code a b c 
0  0 8 4 8 
1  1 1 1 0 
2  2 5 5 2 
3  3 2 2 8 
4  4 3 2 2 

注意兩組數據具有獨特Code列。

解決方案:

In [44]: db_df = pd.read_sql('select * from my_table', engine) 
    ...: missing = db_df.loc[~db_df.Code.isin(df.Code)] 
    ...: print(missing) 
    ...: 
    Code a b c 
5  5 2 8 1 
6  6 5 1 8 
7  7 8 9 7 
8  8 0 7 3 
9  9 2 6 3 

UPDATE:

~是布爾指數的否定:

In [45]: db_df.Code.isin(df.Code) 
Out[45]: 
0  True 
1  True 
2  True 
3  True 
4  True 
5 False 
6 False 
7 False 
8 False 
9 False 
Name: Code, dtype: bool 

In [46]: ~db_df.Code.isin(df.Code) 
Out[46]: 
0 False 
1 False 
2 False 
3 False 
4 False 
5  True 
6  True 
7  True 
8  True 
9  True 
Name: Code, dtype: bool 
+0

感謝@MaxU,是想知道,'〜'是什麼意思?另外,是否有可能將'missing'構造爲'boolean'響應?這樣我就可以做'如果缺少:#做代碼'? –

+1

@jakewong,請檢查更新 – MaxU

相關問題