目录

1.数据框字段类型查看:df.dtypes

2.维度查看df.shape:

3.数据框的策略基本信息df.info():

4.某一列格式df['列名'].dtype:

5.数据类型转换.astype:


Pandas所支持的数据类型: 

Python,numpy都有自己的一套数据格式,它们之间的对应关系可参考如下表格:

pandas默认的数据类型是int64,float64。

1.数据框字段类型查看:df.dtypes


 数据框td_link_data如下

print(td_link_data)

    链路ID  管理域   日期   时间  上行速率Mbps  上行对比速率Mbps  下行速率Mbps  下行对比速率Mbps  上行丢弃速率Mbps  
0     500  10001  20210609  10     0.000         0.011              0.000          0.001             0.0        
1     500  10001  20210609  11     0.000         0.007              0.000          0.000             0.0        
2     500  10001  20210609  12     0.000         0.028              0.000          0.002             0.0        
3     500  10001  20210609  13     0.000         0.056              0.000          0.003             0.0        
4     500  10001  20210609  14     0.000         0.062              0.000          0.003             0.0        
5     500  10001  20210609  15     0.000         0.074              0.000          0.005             0.0        
6     500  10001  20210609  16     0.000         0.061              0.000          0.004             0.0        
7     500  10001  20210609  17     0.000         0.069              0.000          0.004             0.0        
8     500  10001  20210609  18     0.000         0.054              0.000          0.002             0.0        
9     500  10001  20210609  19     0.000         0.054              0.000          0.002             0.0        
10    500  10001  20210609  20     0.000         0.040              0.000          0.004             0.0  
...   ...   ...     ...     ...     ...           ...                ...            ...              ...
...   ...   ...     ...     ...     ...           ...                ...            ...              ...
...   ...   ...     ...     ...     ...           ...                ...            ...              ...
239   500  10001  20210609  23     0.000         0.040              0.000          0.004             0.0     

查看数据框td_link_data中数据类型df.dtypes

print(td_link_data.dtypes)

结果: 

链路ID            int64
管理域             int64
日期             object
时间             object
上行速率Mbps      float64
上行对比速率Mbps    float64
下行速率Mbps      float64
下行对比速率Mbps    float64
上行丢弃速率Mbps    float64
dtype: object

2.维度查看df.shape:


print(td_link_data.shape)

 结果: 说明此数据框一共有240行,9列:

 (240, 9)

3.数据框的策略基本信息df.info():


维度、列名称、数据格式、所占空间等

print(td_link_data.info())

结果:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 240 entries, 0 to 239
Data columns (total 9 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   链路ID        240 non-null    int64  
 1   管理域         240 non-null    int64  
 2   日期          240 non-null    object 
 3   时间          240 non-null    object 
 4   上行速率Mbps    240 non-null    float64
 5   上行对比速率Mbps  240 non-null    float64
 6   下行速率Mbps    240 non-null    float64
 7   下行对比速率Mbps  240 non-null    float64
 8   上行丢弃速率Mbps  240 non-null    float64
dtypes: float64(5), int64(2), object(2)
memory usage: 17.0+ KB

解释:

1.数据类型:数据框 <class 'pandas.core.frame.DataFrame'>
2.表格的维度:240行x9列,RangeIndex:0-239
3.表格的列名,是否为空值和列字段类型dtype
4.数据框包含的字段类型及数量: float64(5), int64(2), object(2)
5.表格所占空间:17.0+ KB

4.某一列格式df['列名'].dtype:


print(td_link_data['管理域'].dtype)

结果:

 int64

需要强调的是object类型实际上可以包括多种不同的类型,比如一列数据里,既有整型、浮点型,也有字符串类型,这些在pandas中都会被标识为‘object’,所以在处理数据时,可能需要额外的一些方法提前将这些字段做清洗,str.replace(),float(),int(),astype(),apply()等等。

5.数据类型转换.astype:


df.index.astype('int64') # 索引类型转换

df.astype('int64') # 所有数据转换为 int64

df.astype('int64', copy=False) # 不与原数据关联

td_link_data.astype({'管理域': 'int32'}) # 指定字段转指定类型

td_link_data['管理域'].astype('float')   #某一列转换

td_link_data['链路ID'].astype('object') #某一列转换

 
参考链接:https://www.jianshu.com/p/8a5f0710cad3

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐