Convert df value of specific columns to string if the digits of the df value in those columns is greater than or equal to 12

Im trying to convert those values in a few columns to string if the number of digits of those numbers are greater than to equal to 12.

I am doing this so as to avoid scientific notation display when I write to excel of these numbers.

Code I tried :-

def count_digits(string):
 if string is None:
    return 0
 string=str(string)
 return sum(item.isdigit() for item in string)

if df['col1'].apply(count_digits) or df['col2'].apply(count_digits) or df['col3'].apply(count_digits) >= 12:
  df.values=str(df.values())

Sample df :-

Param  col1      col2         col3
A      56465780  56.678       None
B      19937000  430624000    26847651.79
D      4457000   999999999999 None
H      256000    344578000    None
K      225000    35           NaN
E      219000    NaN          None

Answer

If I understand correctly try:

def count_digits(string):
    if pd.isna(string):
        return ''
    elif (isinstance(string,(float,int))) or (string.isnumeric()):
        if len(str(string))>=12:
            return str(string)
        else:
            return string
    else:
        return string

Finally:
df.replace({'None':None,'nan':float('NaN')}).applymap(count_digits)

output:

  Param      col1                col2         col3
0     A  56465780  56.677999999999997             
1     B  19937000         430624000.0  26847651.79
2     D   4457000      999999999999.0             
3     H    256000         344578000.0             
4     K    225000                35.0             
5     E    219000                                

OR

If you don’t want to count numbers after decimal . as the length then use:

def count_digits(string):
    if pd.isna(string):
        return ''
    elif (isinstance(string,(float,int))) or (string.isnumeric()):
        if len(str(string).split('.')[0])>=12:
            return str(string)
        else:
            return string
    else:
        return string