python生成dataframe
时间: 2023-08-25 18:08:35 浏览: 100
用Python玩转数据 04_4-dataframe.mp4
可以使用四种方法在Python中创建DataFrame:
1. 由数组/list组成的字典创建DataFrame
```
import pandas as pd
import numpy as np
data = pd.DataFrame({'name':['wencky','stany','barbio'], 'age':[29,29,3], 'gender':['w','m','m']})
```
结果为:
```
age gender name
0 29 w wencky
1 29 m stany
2 3 m barbio
```
2. 利用列表list来构造DataFrame
```
data = [[1, 'a'], [2, 'b'], [3, 'c']]
df = pd.DataFrame(data)
```
或者
```
data = np.array([[1, 'a'], [2, 'b'], [3, 'c']])
df = pd.DataFrame(data)
```
或者
```
data = [np.array([1, 'a']),(2, 'b'),pd.Series([3, 'c'])]
df = pd.DataFrame(data)
```
3. 利用列表和字典结合的方法来构造DataFrame
```
data = [{"column1": 1, "column2": 'a'}, {"column1": 2, "column2": 'b'}, {"column1": 3, "column2": 'c'}]
df = pd.DataFrame(data,index=['index1', 'index2', 'index3'])
```
4. 利用字典dictionary来构造DataFrame
```
data = {'column1': [1, 2, 3], 'column2': ['a', 'b', 'c']}
df = pd.DataFrame(data)
```
或者
```
data = {"column1": [1, 2, 3], "column2": pd.Series(['a', 'b', 'c'])}
df = pd.DataFrame(data)
```
或者
```
data = {"column1": (1, 2, 3), "column2": np.array(['a', 'b', 'c']) }
df = pd.DataFrame(data)
```
阅读全文