python查看各列数据类型_如何使用单个命令[Python – Pandas]获取所有列的数据类型?...

10 min to pandas有一个很好的例子:

DataFrame.dtypes:

df2 = pd.DataFrame({

'A' : 1.,

'B' : pd.Timestamp('20130102'),

'C' : pd.Series(1,index=list(range(4)),dtype='float32'),

'D' : np.array([3] * 4,dtype='int32'),

'E' : pd.Categorical(["test","train","test","train"]),

'F' : 'foo' })

print (df2)

A B C D E F

0 1.0 2013-01-02 1.0 3 test foo

1 1.0 2013-01-02 1.0 3 train foo

2 1.0 2013-01-02 1.0 3 test foo

3 1.0 2013-01-02 1.0 3 train foo

print (df2.dtypes)

A float64

B datetime64[ns]

C float32

D int32

E category

F object

dtype: object

但是使用dtypes = object它有点复杂(通常,显然它是字符串):

样品:

df = pd.DataFrame({'strings':['a','d','f'],

'dicts':[{'a':4}, {'c':8}, {'e':9}],

'lists':[[4,8],[7,8],[3]],

'tuples':[(4,8),(7,8),(3,)],

'sets':[set([1,8]), set([7,3]), set([0,1])] })

print (df)

dicts lists sets strings tuples

0 {'a': 4} [4, 8] {8, 1} a (4, 8)

1 {'c': 8} [7, 8] {3, 7} d (7, 8)

2 {'e': 9} [3] {0, 1} f (3,)

所有值都具有相同的dtypes:

print (df.dtypes)

dicts object

lists object

sets object

strings object

tuples object

dtype: object

但是类型不同,如果需要通过循环检查:

for col in df:

print (df[col].apply(type))

0

1

2

Name: dicts, dtype: object

0

1

2

Name: lists, dtype: object

0

1

2

Name: sets, dtype: object

0

1

2

Name: strings, dtype: object

0

1

2

Name: tuples, dtype: object

或者使用iat的列的第一个值:

print (type(df['strings'].iat[0]))

print (type(df['dicts'].iat[0]))

print (type(df['lists'].iat[0]))

print (type(df['tuples'].iat[0]))

print (type(df['sets'].iat[0]))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值