我的数据帧的最低值为零.我试图使用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')
我已经尝试将精度设置为-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')
有没有办法直接用pandas.cut()获得非负整数作为区间边界而不使用numpy?
编辑:我只是注意到指定right = False使最低间隔移到0而不是-0.4.它似乎优先于include_lowest,因为改变后者与right = False没有任何明显的效果.仍然使用一个小数点指定以下间隔.