1

I need to find out the datatype of columns with null values only.

df.columns[df_test.isnull().sum()>0] 

is giving me the list of columns which has null values , but i need the list of columns with datatypes.

Can you please help !!!

2 Answers 2

2

Based on what I understand, you can use loc directly without using any loop.

df_test.loc[:,df_test.isnull().sum()>0].dtypes

An example below:

df = pd.util.testing.makeMissingDataframe().fillna({"A":0}).head(5)
print(df)
         A         B         C         D
UxNqePUQXH  0.449476  0.713696  1.255964 -0.458908
qBsuuVjimE  0.840831  0.439097  1.073890  1.252807
ptZvWjEdUV  0.377418  0.061335  0.960726  1.140141
LO54c7vqq1 -1.708196 -0.483487  0.831404       NaN
Pc7I7gTRZY       NaN -1.400275  0.045297 -0.322303

>>df.loc[:,df.isnull().sum()>0].dtypes

C    float64
D    float64
dtype: object

Note that you can also explore any:

df_test.loc[:,df_test.isnull().any()].dtypes.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Really appreciate it.
0
import pandas as pd

for x in df:
    if df[x].isna().any():
        print(df[x].dtype)

that is if the column contains a single null value. if you only want the datatype if ALL the values in the column are null, you can do the below

import pandas as pd

for x in df:
    if df[x].isna().all():
        print(df[x].dtype)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.