pandas的数据框如何定义
时间: 2023-02-22 18:29:16 浏览: 0
pandas数据框,统计某列数据对应的个数方法
Pandas 的数据框是一种二维数据结构,类似于电子表格或 SQL 表。它可以通过以下方式定义:
1. 通过列表或字典来创建:
```
import pandas as pd
data = {"name": ["John", "Jane", "Jim"],
"age": [32, 28, 40],
"city": ["New York", "London", "Paris"]}
df = pd.DataFrame(data)
```
2. 通过 Numpy 数组创建:
```
import pandas as pd
import numpy as np
data = np.array([[32, "John", "New York"],
[28, "Jane", "London"],
[40, "Jim", "Paris"]])
df = pd.DataFrame(data, columns=["age", "name", "city"])
```
3. 通过已有的数据框创建:
```
import pandas as pd
df1 = pd.DataFrame({"name": ["John", "Jane", "Jim"],
"age": [32, 28, 40],
"city": ["New York", "London", "Paris"]})
df2 = pd.DataFrame(df1)
```
阅读全文