简体   繁体   中英

datatype in python (looking for something similar to str in R)

I want to find the datatype of all the variables in a csv file in python. In R we can achieve the same using str() command .

str(data_frame)

this gives an output like this

> str(train)
'data.frame':   891 obs. of  12 variables:
 $ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Survived   : int  0 1 1 1 0 0 0 0 1 1 ...
 $ Pclass     : int  3 1 3 1 3 3 1 3 3 2 ...
 $ Name       : Factor w/ 891 levels "Abbing, Mr. Anthony",..: 109 191 358 277 16 559 520 629 417 581 ...
 $ Sex        : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
 $ Age        : num  22 38 26 35 35 NA 54 2 27 14 ...
 $ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Ticket     : Factor w/ 681 levels "110152","110413",..: 524 597 670 50 473 276 86 396 345 133 ...
 $ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Cabin      : Factor w/ 148 levels "","A10","A14",..: 1 83 1 57 1 1 131 1 1 1 ...
 $ Embarked   : Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ...

is there a similar way in python?

You probably want dtypes

>>> import pandas as pd
>>> df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [1.0, 2.0, 3.0], 'baz': ['qux', 'quux', 'quuux']})
>>> df.dtypes
bar    float64
baz     object
foo      int64
dtype: object

An easy way to tell if a string represents a valid int is to simply attempt to convert the string to an int and catch the ValueError exception if it isn't a legal int . Similarly with float . Here's a brief demo in Python 2:

data = 'string 37 3.14159 word -5 0 -1.4142 text'

def datatype(s):
    try:
        int(s)
    except ValueError:
        try:
            float(s)
        except ValueError:
            return 'string'
        else:
            return 'float'
    else:
        return 'int'

for s in data.split():
    print '%-15r: %s' % (s, datatype(s))

output

'string'       : string
'37'           : int
'3.14159'      : float
'word'         : string
'-5'           : int
'0'            : int
'-1.4142'      : float
'text'         : string

However, normal Python code (generally) wouldn't use a function quite like that: it would assume that the data is correct and wrap the conversion code in a simple try: ... except ValueError:... else: block rather than using that crazy nested structure to test data before you're ready to process it.

A sensible CSV won't have different datatypes in random positions, so your code shouldn't need to guess what type of data is in a give field. OTOH, not all CSV's are well-designed... :)

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