利用字典和Series分别创建2个二维数据表DataFrame。
时间: 2024-05-12 15:13:17 浏览: 71
根据数据字典生成数据库表
可以使用字典创建DataFrame,如下所示:
```python
import pandas as pd
data = {"name": ["Alice", "Bob", "Charlie", "David"],
"age": [25, 30, 35, 40],
"gender": ["F", "M", "M", "M"]}
df = pd.DataFrame(data)
print(df)
```
输出结果为:
```
name age gender
0 Alice 25 F
1 Bob 30 M
2 Charlie 35 M
3 David 40 M
```
可以使用Series创建DataFrame,如下所示:
```python
import pandas as pd
name = pd.Series(["Alice", "Bob", "Charlie", "David"])
age = pd.Series([25, 30, 35, 40])
gender = pd.Series(["F", "M", "M", "M"])
data = {"name": name, "age": age, "gender": gender}
df = pd.DataFrame(data)
print(df)
```
输出结果与前面相同。
阅读全文