python 3.x - Cell wise operation on data frame, determine precision -


i have data frame different data types in it. determine precision of float types. can select float64 code:

df_float64 = df.loc[:, df.dtypes == np.float64] 

(not sure why columns 'nan' values selected side note)

now determine precision thing abut such approach:

precision = len(cell.split(".")[1] 

if cell string.

and have output in form of csv maximum precision each column.

so having data frame this:

|     a|     b|     c|     d| |  0.01|0.0923|   1.0|   1.2| | 100.1| 203.3| 1.093|   1.9| |   0.0|  0.23|  1.03|   1.0| 

i to have this:

|     a|     b|     c|     d| |     2|     4|     3|     1| 

is possible using pandas?

thanks

you can use:

  • fillna first remove nans
  • cast str astype
  • loop columns apply or list comprehension lambda function
  • for each column split, second values of list str[1] , len
  • get max values - output series
  • convert series 1 row dataframe if necessery

a = df.fillna(0).astype(str).apply(lambda x: x.str.split('.').str[1].str.len()).max() print (a)    2 b    4 c    3 d    1 dtype: int64  df = a.to_frame().t print (df)     b  c  d 0  2  4  3  1 

another solution:

df = df.fillna(0).astype(str) = [df[x].str.split('.').str[1].str.len().max() x in df]  df = pd.dataframe([a], columns=df.columns) print (df)     b  c  d 0  2  4  3  1 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -