xxxxxxxxxx
# displays max number of columns and rows
import pandas as pd
pd.options.display.max_rows = 999
pd.options.display.max_columns = 999
xxxxxxxxxx
# Find the maximum value in the DataFrame
max_value = df.max().max()
# find column and index
max_location = df.stack().idxmax()
column_name = max_location[1]
index_value = max_location[0]
# Alternative approach
# Flatten the DataFrame and sort the values
flat_df = df.unstack().reset_index()
flat_df.columns = ['Column', 'Index', 'Value']
# Sort by value and get the second largest
second_largest = flat_df.nlargest(2, 'Value').iloc[1]
# Retrieve the column name and index
column_name = second_largest['Column']
index_value = second_largest['Index']
second_largest_value = second_largest['Value']
xxxxxxxxxx
upvote if u've been studing french for 12 years and u still need google traduction
xxxxxxxxxx
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['abc', 'de', 'abcd'],
'B': ['a', 'abcde', 'abc'],
'C': [1, 2.5, 1.5]})
measurer = np.vectorize(len)
# All Columns
res1 = dict(zip(df, measurer(df.values.astype(str)).max(axis=0)))
{'A': 4, 'B': 5, 'C': 3}
# Columns that only data type = object
df_object = df.select_dtypes(include=[object])
res2 = dict(zip(df_object, measurer(df_object.values.astype(str)).max(axis=0)))
{'A': 4, 'B': 5}
xxxxxxxxxx
# credit to Stack Overflow in the source link
df.head()
Communications and Search Business General Lifestyle
0 0.745763 0.050847 0.118644 0.084746
0 0.333333 0.000000 0.583333 0.083333
0 0.617021 0.042553 0.297872 0.042553
0 0.435897 0.000000 0.410256 0.153846
0 0.358974 0.076923 0.410256 0.153846
df.idxmax(axis=1)
0 Communications
1 Business
2 Communications
3 Communications
4 Business
dtype: object