python中合法的二进制整数_python – 使用`pandas.cut()`,如何获得整数二进制数并避免获得负下限?...

我的数据帧的最低值为零.我试图使用pandas.cut()的precision和include_lowest参数,但我不能得到整数而不是带有一个小数的浮点数.我也不能让最左边的间隔停在零.

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt

sns.set(style='white', font_scale=1.3)

df = pd.DataFrame(range(0,389,8)[:-1], columns=['value'])

df['binned_df_pd'] = pd.cut(df.value, bins=7, precision=0, include_lowest=True)

sns.pointplot(x='binned_df_pd', y='value', data=df)

plt.xticks(rotation=30, ha='right')

FnUwZ.png

我已经尝试将精度设置为-1,0和1,但它们都输出一个十进制浮点数. pandas.cut()帮助确实提到x-min和x-max值扩展了x范围的0.1%,但我认为include_lowest可能会以某种方式抑制这种行为.我目前的解决方法涉及导入numpy:

import numpy as np

bin_counts, edges = np.histogram(df.value, bins=7)

edges = [int(x) for x in edges]

df['binned_df_np'] = pd.cut(df.value, bins=edges, include_lowest=True)

sns.pointplot(x='binned_df_np', y='value', data=df)

plt.xticks(rotation=30, ha='right')

9fVUT.png

有没有办法直接用pandas.cut()获得非负整数作为区间边界而不使用numpy?

编辑:我只是注意到指定right = False使最低间隔移到0而不是-0.4.它似乎优先于include_lowest,因为改变后者与right = False没有任何明显的效果.仍然使用一个小数点指定以下间隔.

CBgpA.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值