Home > Net >  How to get datatypes of each row using pandas?
How to get datatypes of each row using pandas?

Time:07-07

I have a dataframe that looks like this.

Name Age
John 20.5
Alice 39.1
Pam 41.0
203921 Hope

I want to create a new column called "Name_Type" that returns as follows:

Name Name_Type
John True
Alice True
Pam True
203921 False
Age Age_Type
20.5 True
39.1 True
41.0 True
Hope False

I want to check if the column, Name, is a STRING. I will do the same for Age, checking if it is a FLOAT.

CodePudding user response:

I expect this to work in your case:

df = pd.DataFrame({'Name':['John', 'Alice', 'Pam', 203921], 'Age':[20.5, 39.1, 41.0, 'Hope']})
df['Name_Type'] = [True if type(x) == str else False for x in df.Name]
df['Age_Type'] = [True if type(x) == float else False for x in df.Age]
print(df)

Result:

     Name   Age  Name_Type  Age_Type
0    John  20.5       True      True
1   Alice  39.1       True      True
2     Pam  41.0       True      True
3  203921  Hope      False     False

When all values are strings, this solution should work:

df = pd.DataFrame({'Name':['John', 'Alice', 'Pam', '203921'], 'Age':['20.5', '39.1', '41.0', 'Hope']})

def is_float(x):
    try:
        float(x)
        return True
    except:
        return False

df['Name_Type'] = ~df.Name.str.isnumeric()    
df['Age_Type'] = df.Age.apply(is_float)
  • Related