连接/合并在python中不起作用

2024-09-30 03:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

试图加入df61和dfèpetsyègz的picè代码。我还包括了变量的数据类型。我的代码输出一堆NaN,表示两个数据集之间没有picu代码匹配。有几百万行数据,所以我肯定有很多匹配的。我想我做错了什么。你知道吗

df61.head(3)

 mpe_wgt                        pic_code
      10  420336479305589843900801597032
      10  420907139300189843900792911982
      10  420967449300189843900797682603

mpe_wgt     object
pic_code    object


df_petsy_gz.head(3)

monthly_fiscal_year  month                        pic_code class_of_mail  
               2017     11  420606019300189843900566128707            FC   
               2017     11  420731629300189843900584700299            FC   
               2017     11  420405029300189843900568579224            FC   

   weight  calc_postage  calc_total_postage  MikeZone  
   0.8750          4.02                4.02         5  
   0.3750          2.77                2.77         6  
   0.6875          3.60                3.60         8 

 monthly_fiscal_year      int64
 month                    int64
 pic_code                object
 class_of_mail           object
 weight                 float64
 calc_postage           float64
 calc_total_postage     float64
 MikeZone                 int64

 df61_mpe=pd.merge(df_petsy_gz,df61,on='pic_code', how='outer')

输出

monthly_fiscal_year  month                        pic_code class_of_mail  \
             2017.0   11.0  420606019300189843900566128707            FC   
             2017.0   11.0  420731629300189843900584700299            FC   
             2017.0   11.0  420405029300189843900568579224            FC   
             2017.0   11.0  420301349300189843900567382542            FC   

   weight  calc_postage  calc_total_postage  MikeZone mpe_wgt  
   0.8750          4.02                4.02       5.0     NaN  
   0.3750          2.77                2.77       6.0     NaN  
   0.6875          3.60                3.60       8.0     NaN  
   0.5000          2.77                2.77       4.0     NaN  

Tags: 代码dfobjectcodecalcnanfcgz
1条回答
网友
1楼 · 发布于 2024-09-30 03:25:44

我不知道你的数据是什么样子,但我只知道连接的类型会影响连接后行的形成。你知道吗

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.join.html

how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default: ‘left’

How to handle the operation of the two objects.

left: use calling frame’s index (or column if on is specified)
right: use other frame’s index
outer: form union of calling frame’s index (or column if on is specified) with other frame’s index, and sort it lexicographically
inner: form intersection of calling frame’s index (or column if on is specified) with other frame’s index, preserving the order of the calling’s one

尝试使用'inner'连接,看看这是否是您所需要的。
这将只返回在两个数据帧中都有pic\u code且具有mpe\u wgt的行。

另外,要确保picu代码没有尾随/前导空格,以便来自两个数据帧的类似picu代码匹配。你知道吗

相关问题 更多 >

    热门问题