Pandas merge后全为空Nan

问题:

我有两个如下的dataframe,index也是相同的

           logFC     AveExpr          t       P.Value  adj.P.Val         B
31124   3.284541    1.648456  50.427905  4.012965e-07   0.011210  7.769927
19930   1.549249    0.843535  28.414880  4.688255e-06   0.043655  5.321548
40422  17.256854    8.628910  19.096341  2.555958e-05   0.118327  3.404473
39569  14.772801   19.998569  14.766398  7.607675e-05   0.193197  2.122275
20181  68.110474  450.855255  14.273843  8.779938e-05   0.213272  1.952015
...          ...         ...        ...           ...        ...       ...
8708   32.318876  130.330466   4.438732  9.639254e-03   0.865688 -3.691248
17753  11.930959   26.719863   4.433230  9.682917e-03   0.866947 -3.696619
28713   1.127502    1.989897   4.420363  9.785952e-03   0.868792 -3.709198
4315    4.658730   15.094117   4.415421  9.825873e-03   0.868792 -3.714035
28263   3.949047    7.173845   4.399925  9.952321e-03   0.874255 -3.729227

[151 rows x 6 columns]
            Fcon1       Fcon2       Fcon3       FTAI1       FTAI2       FTAI3 up/down Official_Symbol
363      3.968901    4.043801    3.329393    2.651096    2.495120    2.267443      up         Plekha1
745    137.437256  137.844254  147.885880  113.702637  110.335915  100.270744      up       Serpinb6a
933      6.431833    5.565875    3.619443    0.000000    0.000000    0.000000      up          Tardbp
1187    31.972027   28.155848   29.948334   20.580564   21.249414   18.863104      up          Fchsd2
1190     1.995128    1.429263    1.321291    0.567278    0.535526    0.485705      up          Crebrf
...           ...         ...         ...         ...         ...         ...     ...             ...
54043  443.697540  448.293732  468.942200  354.451294  374.866638  378.524048      up         Cyp4f14
54250   29.244991   27.789938   31.440329   23.169428   22.493416   20.367746      up           Itga6
54349    5.590240    6.356446    7.214879    3.743275    1.146106    0.748171      up             Ddc
55507    2.693715    2.128844    2.108521    0.000000    0.000000    0.000000      up          Mindy3
55641    6.026206    7.566981    7.150218    2.788216    2.971823    2.464447      up          Parp11

[151 rows x 8 columns]

merge后

DEG_up = pd.merge(DEG_up,result_df,how='left',left_index=True,right_index=True)
---

            Fcon1       Fcon2       Fcon3       FTAI1       FTAI2       FTAI3 up/down Official_Symbol  logFC  AveExpr   t  P.Value  adj.P.Val   B
363      3.968901    4.043801    3.329393    2.651096    2.495120    2.267443      up         Plekha1    NaN      NaN NaN      NaN        NaN NaN
745    137.437256  137.844254  147.885880  113.702637  110.335915  100.270744      up       Serpinb6a    NaN      NaN NaN      NaN        NaN NaN
933      6.431833    5.565875    3.619443    0.000000    0.000000    0.000000      up          Tardbp    NaN      NaN NaN      NaN        NaN NaN
1187    31.972027   28.155848   29.948334   20.580564   21.249414   18.863104      up          Fchsd2    NaN      NaN NaN      NaN        NaN NaN
1190     1.995128    1.429263    1.321291    0.567278    0.535526    0.485705      up          Crebrf    NaN      NaN NaN      NaN        NaN NaN
...           ...         ...         ...         ...         ...         ...     ...             ...    ...      ...  ..      ...        ...  ..
54043  443.697540  448.293732  468.942200  354.451294  374.866638  378.524048      up         Cyp4f14    NaN      NaN NaN      NaN        NaN NaN
54250   29.244991   27.789938   31.440329   23.169428   22.493416   20.367746      up           Itga6    NaN      NaN NaN      NaN        NaN NaN
54349    5.590240    6.356446    7.214879    3.743275    1.146106    0.748171      up             Ddc    NaN      NaN NaN      NaN        NaN NaN
55507    2.693715    2.128844    2.108521    0.000000    0.000000    0.000000      up          Mindy3    NaN      NaN NaN      NaN        NaN NaN
55641    6.026206    7.566981    7.150218    2.788216    2.971823    2.464447      up          Parp11    NaN      NaN NaN      NaN        NaN NaN

[151 rows x 14 columns] 

解决:

打印index的类型查看,发现不一致

print (DEG_up.index)
print (result_df.index)

---
Int64Index([  363,   745,   933,  1187,  1190,  1603,  1627,  2192,  2310,
             2492,
            ...
            52788, 52990, 53373, 53681, 53829, 54043, 54250, 54349, 55507,
            55641],
           dtype='int64', length=151)
Index(['31124', '19930', '40422', '39569', '20181', '5572', '5625', '55507',
       '9178', '41531',
       ...
       '31718', '28243', '25591', '54349', '35914', '8708', '17753', '28713',
       '4315', '28263'],
      dtype='object', length=151)

将索引的类型设置为一致的

DEG_up.index = DEG_up.index.astype("int64")
result_df.index = result_df.index.astype("int64")
---
            Fcon1       Fcon2       Fcon3       FTAI1       FTAI2       FTAI3  ...      logFC     AveExpr          t   P.Value  adj.P.Val         B
363      3.968901    4.043801    3.329393    2.651096    2.495120    2.267443  ...   1.309479    3.125959   5.373214  0.004718   0.701119 -2.838175
745    137.437256  137.844254  147.885880  113.702637  110.335915  100.270744  ...  32.952698  124.579448   6.459141  0.002304   0.577388 -1.976445
933      6.431833    5.565875    3.619443    0.000000    0.000000    0.000000  ...   5.205717    2.602858   6.487810  0.002264   0.577388 -1.955324
1187    31.972027   28.155848   29.948334   20.580564   21.249414   18.863104  ...   9.794376   25.128215   7.739672  0.001116   0.445565 -1.102235
1190     1.995128    1.429263    1.321291    0.567278    0.535526    0.485705  ...   1.052391    1.055698   5.186436  0.005399   0.723379 -2.999702
...           ...         ...         ...         ...         ...         ...  ...        ...         ...        ...       ...        ...       ...
54043  443.697540  448.293732  468.942200  354.451294  374.866638  378.524048  ...  84.363831  411.462575   8.105698  0.000925   0.434123 -0.875606
54250   29.244991   27.789938   31.440329   23.169428   22.493416   20.367746  ...   7.481556   25.750975   5.719150  0.003710   0.648525 -2.549867
54349    5.590240    6.356446    7.214879    3.743275    1.146106    0.748171  ...   4.508004    4.133186   4.450474  0.009547   0.863565 -3.679799
55507    2.693715    2.128844    2.108521    0.000000    0.000000    0.000000  ...   2.310360    1.155180  12.486301  0.000154   0.269285  1.279699
55641    6.026206    7.566981    7.150218    2.788216    2.971823    2.464447  ...   4.172973    4.827982   8.945901  0.000618   0.351973 -0.388451

[151 rows x 14 columns]

你可能感兴趣的:(pandas,数据分析,数据挖掘)