fork download
  1. def removefirstlastchar(x):
  2. value = str(x)
  3. if value.lower() == 'NAN'.lower():
  4. return x
  5. if(value[0].isalpha() == True):
  6. value = value[1:]
  7. if(value[-1].isalpha() == True):
  8. value = value[:-1]
  9. return float(value)
  10.  
  11.  
  12.  
  13. # I am passing the above function to the columns 74:92 in my dataframe
  14. df.iloc[:, 74:92] = df.iloc[:, 74:92].apply(removefirstlastchar)
  15.  
  16. # This is not cleaning. it says "could not convert string to float: '0 O15.5\\n1 NaN\\n2 NaN\\n3 NaN\\n4 NaN\\n5 15.4K\\n6 NaN\\n7 NaN\\n8 NaN\\n9 NaN\\n10 NaN\\n11 NaN\\n12 NaN\\n13 NaN\\n14 NaN\\n15 NaN\\n16 Q10.4\\n17 NaN\\n18 NaN\\n19 NaN\\n20 NaN\\n21 NaN\\n22 NaN\\n23 S16.7\\n24 NaN\\n25 NaN\\n26 NaN\\n27 NaN\\n28 NaN\\n29 14.5\\n30 14.3\\n31 NaN\\n32 NaN\\n33 14.6P\\n34 NaN\\n35 NaN\\n36 NaN\\nName: depression_adult, dtype: objec'", 'occurred at index depression_adult')
  17.  
  18.  
Runtime error #stdin #stdout #stderr 0.05s 118656KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 14, in <module>
    df.iloc[:, 74:92] = df.iloc[:, 74:92].apply(removefirstlastchar)
NameError: global name 'df' is not defined