Postgresql:更新特定列表中数据帧python中的数据



我的postgresql数据库中有一个名为"abc";

id | name | age | hobby | address | status |
1  | ana  | 32  | eat   | NULL    |  NULL  | 
2  | mela | 43  | play  | NULL    |  NULL  | 
3  | ani  | 50  | play  | NULL    |  NULL  | 
4  | jen  | 23  | read  | NULL    |  NULL  | 
5  | sop  | 14  | sleep | NULL    |  NULL  |
...etc

并且我在熊猫中具有名为"的数据帧;aadc";

id | address | status   |
1  | jambi   | single   |
2  | tgr     | married  |
3  | jakarta | divorced |
4  | jaksel  | single   |
5  | meruya  | divorced |
..etc

但我实际上期望在数据库表"中得到这种输出;abc";

id | name | age | hobby | address | status   |
1  | ana  | 32  | eat   | jambi   | single   |
2  | mela | 43  | play  | tgr     | married  | 
3  | ani  | 50  | play  | jakarta | divorced |
4  | jen  | 23  | read  | jaksel  | single   |
5  | sop  | 14  | sleep | meruya  | divorced |
...etc

如何更新表";abc";在我的postgredb from dataframe中;aadc">

先删除空列,然后用左联接合并:

df = df1.drop(['address','status'], axis=1).merge(df2, on='id', how='left')

或者用DataFrame.combine_first:中的id替换缺失值

df = df1.set_index('id').combine_first(df2.set_index('id')).reset_index()
print (df)
id  address  age  hobby  name    status
0   1    jambi   32    eat   ana    single
1   2      tgr   43   play  mela   married
2   3  jakarta   50   play   ani  divorced
3   4   jaksel   23   read   jen    single
4   5   meruya   14  sleep   sop  divorced

最新更新