[英]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. 我想在python中找到csv文件中所有变量的数据类型。 In R we can achieve the same using
str()
command . 在R中,我们可以使用
str()
命令实现相同的功能。
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? 在python中有类似的方式吗?
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
. 判断字符串是否表示有效
int
的简单方法是简单地尝试将字符串转换为int
并捕获ValueError
异常(如果它不是合法的int
。 Similarly with float
. 与
float
类似。 Here's a brief demo in Python 2: 这是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. 但是,正常的Python代码(通常)不会使用这样的函数:它会假设数据是正确的并且在简单的
try: ... except ValueError:... else:
包装转换代码try: ... except ValueError:... else:
block而不是在准备好处理数据之前,使用这个疯狂的嵌套结构来测试数据。
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. 合理的CSV在随机位置不会有不同的数据类型,因此您的代码不需要猜测给定字段中的数据类型。 OTOH, not all CSV's are well-designed... :)
OTOH,并非所有的CSV都是精心设计的...... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.