简体   繁体   中英

How to get datatypes of each row using pandas?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM