Home > Software engineering >  Python Dataframe Find the length of maximum element in a dataframe
Python Dataframe Find the length of maximum element in a dataframe

Time:12-23

I am trying to find the length (number of digits) in the maximum value of a data frame. Why? Then I know how much y-axis ticks will extend when plotting this data, and accordingly, I can adjust the plot's left border.

My code:

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()

is there a better way to find the maximum length of the element?

CodePudding user response:

You can do this with the lambda, first find the max value in the dataframe, the cast to string and take 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)

Or

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)

Compared to your solution:

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)
  • Related