[英]Pandas in merge don't return index for right dataset
Is it bug?是虫子吗?
work (GOOD)工作(好)
df.merge(label,left_on='ID',right_on='REQ_ID',how='inner',left_index=True).index
Int64Index([102315, 102316, 142966, 21285, 21283, 1062, 61823, 21274,
82044, 21280,
...
101105, 80927, 141854, 162123, 80937, 80878, 141842, 121531,
162107, 162117],
dtype='int64', length=72231)
work (GOOD)工作(好)
label.merge(df,left_on='REQ_ID',right_on='ID',how='inner',left_index=True).index
Int64Index([109713, 109584, 109665, 110019, 109877, 110295, 110232, 110195,
110249, 110397,
...
996301, 788659, 996305, 995726, 995358, 994281, 927879, 980877,
895347, 995278],
dtype='int64', length=72231)
does not work (BAD)不工作(坏)
df.merge(label,left_on='ID',right_on='REQ_ID',how='inner',right_index=True).index
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-120-ba8a8ccff1bd> in <module>
----> 1 df.merge(label,left_on='ID',right_on='REQ_ID',how='inner',right_index=True).index
~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in merge(self, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
7948 from pandas.core.reshape.merge import merge
7949
-> 7950 return merge(
7951 self,
7952 right,
~/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
72 validate=None,
73 ) -> "DataFrame":
---> 74 op = _MergeOperation(
75 left,
76 right,
~/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)
654 # validate the merge keys dtypes. We may need to coerce
655 # to avoid incompatible dtypes
--> 656 self._maybe_coerce_merge_keys()
657
658 # If argument passed to validate,
~/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py in _maybe_coerce_merge_keys(self)
1163 inferred_right in string_types and inferred_left not in string_types
1164 ):
-> 1165 raise ValueError(msg)
1166
1167 # datetimelikes must match exactly
ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
df.dtypes
ID object
label.dtypes
REQ_ID object
Please check documentation https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html : right_index - Use the index from the right DataFrame as the join key. Please check documentation https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html : right_index - Use the index from the right DataFrame as the join key. Same caveats as left_index.
与 left_index 相同的警告。
so you actually try to merge on index of your label dataframe, which is of int64所以你实际上尝试合并你的 label dataframe 的索引,它是 int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.