I am very new to python and I would just like some guidance.
I want to know how to iterate over each value for each column of my data frame to apply a function I have created myself. First I need to check if it is numeric and if yes then I can proceed with my function.
Should I first make each column into lists? If so whats the best way?
CodePudding user response:
you can take a look at this documentation page which explains the use of the function pandas.Dataframe.apply
: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html
Basically, you give it a function func
and an axis (either 0 for columns for ` for rows).
CodePudding user response:
You probably dont want to use a loop for that but instead return a Series containing the information OnlyNumeric/NotOnlyNumeric for every column.
You use pd.to_numeric and coerce the errors like this:
import pandas as pd
df = pd.DataFrame({'col': [1, 2, 10, np.nan, 'a'],
'col2': ['a', 10, 30, 40, 50],
'col3': [1, 2, 3, 4, 5.0]})
df.apply(lambda col: pd.to_numeric(col, errors='coerce').notnull().all())
output:
col False
col2 False
col3 True
dtype: bool
You can then use .all() again to get a single True or False and continue on with applying your function.
in one-line:
df.apply(lambda col: pd.to_numeric(col, errors='coerce').notnull().all()).all()