如何使用DataFrames将类似的数字与范围/条件和合并ID分组?
请,我有一个按升序列出的数据框架。我的目标是平均相似的数字(在“两个方向”中彼此之间的10%以内的数字)在一起,并将其“铃”名称列出。例如,图像显示输入和输出数据框架。我尝试编码它,但我坚持如何进步。
def full_data_compare(self, df_full = pd.DataFrame()):
for k in range(df_full): #current rows
for j in range(df_full): #future rows
if int(df_full['Size'][k]) - int(df_full['Size'][k])*(1/10) <= int(df_full['Size'][j]) <= int(df_full['Size'][k]) + int(df_full['Size'][k])*(1/10) & int(df_full['Size'][k]) - int(df_full['Size'][k])*(1/10) <= int(df_full['Size'][j]) <= int(df_full['Size'][k]) + int(df_full['Size'][k])*(1/10):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您真的想检查两个方向,即连续值在10%以内,则需要使用
pct_change
计算两个系列。然后将其用于groupby.agg
:nb。如果要分组非连续值,则首先需要对它们进行排序:
sort_values(by ='size')
output:
Assuming you really want to check in both directions that the consecutive values are within 10%, you need to compute two Series with
pct_change
. Then use it togroupby.agg
:NB. If you want to group non-consecutive values, you first need to sort them:
sort_values(by='Size')
Output: