Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find no of categorical columns and numerical columns in dataset

Tags:

python

pandas

I need to find number of numerical and categorical columns

Check how many categorical and numerical columns are there

Categorical - object type

Numerical - int,float

Boolean - bool

df = pd.read_csv("titanic.csv")

as i can only get name of the columns with df._get_numeric_data().columns i need sum of the columns

like image 216
venkat0107 Avatar asked Oct 23 '25 18:10

venkat0107


1 Answers

You can use columns = df.applymap(np.isreal).all(), Output will be,

PassengerId     True
Pclass          True
Name           False
Sex            False
Age             True
SibSp           True
Parch           True
Ticket         False
Fare            True
Cabin          False
Embarked       False
dtype: bool

All columns with numarical values will return true, othervice return false.

Also, you can get the true and false count using

print((columns).value_counts())

Output :

True     6
False    5
dtype: int64

Which means is df has 6 numerical and 5 categorical columns.

like image 144
Ransaka Ravihara Avatar answered Oct 25 '25 09:10

Ransaka Ravihara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!