How can I write a for loop to check types of columns in a dataframe one by one? For example, for below df:
Any help would be great!
Edit based on comment
A simpler version based on @NewbieAF's comment: df.dtypes
This should do it
{c: df[c].dtype for c in df.columns}
Example:
import pandas as pd
data = [{'age': 20, 'name': 'james', 'gpa': 18.5},
{'age': 21, 'name': 'anna', 'gpa': 19.6},
{'age': 18, 'name': 'sam', 'gpa': 17.0}]
df = pd.DataFrame(data)
print({c: df[c].dtype for c in df.columns})
Result:
{'age': dtype('int64'), 'name': dtype('O'), 'gpa': dtype('float64')}