Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the dtype of specific column with a loop

I want to display all my column with the dtypes.
For this I used to do df.dtypes. But in this dataframe I have 4000 column and I would also like to number them.

I tried something like this:

for i in range(df.shape[1]):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None):
        print(i ,':',df.dtypes)

This obviously does not give me the expected result but you get the idea of what I need. How can I can use df.dtypes in a loop?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can try this:

for i in range(df.shape[1]):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None):
        print(i ,':',df.iloc[:,i].dtypes)

If you want column name also, then try this:

for i in range(df.shape[1]):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None):
        print(i ,':',df.columns[i],df.iloc[:,i].dtypes)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading