我试图在数据帧的最大值中找到长度(位数)。为什么?这样我就知道在绘制这些数据时y轴刻度将扩展多少,并相应地调整图的左边框。
我代码:
df =
datetime A B C
2022-08-23 06:12:00 1.91 98.35 1.88
2022-08-23 06:13:00 1.92 92.04 1.77
2022-08-23 06:14:00 132.14 81.64 1.75
# maximum length element
max_len = df.round(2).dropna().astype('str').applymap(lambda x:len(x)).max().max()
print(max_len)
6
df.plot(figsize=(5,3),
use_index=True,
colormap=cm.get_cmap('Set1'),
alpha=0.5)
# plot border for saving
left_border = (max_len/100)+0.05
plt.subplots_adjust(left=left_border, right=0.90, top=0.95, bottom=0.25)
plt.savefig(save_dir+plot_df.index[i]+'.jpg',dpi=500)
plt.show()
是否有更好的方法来找到元素的最大长度?
您可以在没有lambda的情况下做到这一点,首先找到数据帧中的最大值,将其转换为字符串并取len:
len(str(df.round(2).max().max()))
# Outputs: 6
%timeit returns: 979 µs ± 17 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
或
len(str(np.max(df.to_numpy())))
# Outputs: 6
%timeit returns: 9.35 µs ± 136 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
与您的解决方案相比:
df.round(2).dropna().astype('str').applymap(lambda x:len(x)).max().max()
6
2.23 ms ± 68.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)