analysis专栏 03.数据处理分析模块 02.DataFrame对象创建

 

# 02.DataFrame对象创建

[toc]{type: "ol", level: [3,4,5]}

### 通过字典创建
```python
import pandas as pd
population_dict = {'beijing': 3000, 'shanghai':1200, 'guangzhou':1800}
area_dict = {'beijing': 300, 'shanghai': 180, 'guangzhou':200, }
# 将两个series对象作为字典的值,就可以创建一个DataFrame对象
population_series = pd.Series(population_dict)
area_series = pd.Series(area_dict)
# pd.DataFrame(population_series, columns=['population']) 等价于下方
citys = pd.DataFrame({'population': population_series, 'area': area_series})
citys
```
![二维数组](imgs/07.png)


### DataFrame属性
```python
citys.index
```
![二维数组](imgs/08.png)
```python
citys.values
```
![二维数组](imgs/09.png)
```python
# 获取列名
citys.columns
```
![二维数组](imgs/10.png)


### 通过列表创建
#### 默认创建
```python
population_dict = {'beijing': 3000, 'shanghai':1200, 'guangzhou':1800}
area_dict = {'beijing': 300, 'shanghai': 180, 'guangzhou':200, }
data = pd.DataFrame([population_dict, area_dict])
data
```
![二维数组](imgs/11.png)

#### 出现不同字段
```python
population_dict = {'beijing': 3000, 'shanghai':1200, 'guangzhou':1800, 'hangzhou': 2400}
area_dict = {'beijing': 300, 'shanghai': 180, 'guangzhou':200, 'shenzhen':280}
data = pd.DataFrame([population_dict, area_dict])
data
```
![二维数组](imgs/12.png)

#### 指定index名称
```python
population_dict = {'beijing': 3000, 'shanghai':1200, 'guangzhou':1800}
area_dict = {'beijing': 300, 'shanghai': 180, 'guangzhou':200, }
data = pd.DataFrame([population_dict, area_dict], index=['population', 'area'])
data
```
![二维数组](imgs/13.png)


### 通过生成器创建
```python
pd.DataFrame([{'a': i, 'b': i*2} for i in range(4)])
```
![二维数组](imgs/14.png)

你可能感兴趣的:(analysis,数据分析)