pandas新建一个dataframe
时间: 2023-04-23 20:05:37 浏览: 110
可以使用pandas中的DataFrame()函数来新建一个DataFrame。例如:
```python
import pandas as pd
data = {'name': ['John', 'Jane', 'Sam'],
'age': [30, 25, 35],
'city': ['New York', 'San Francisco', 'Los Angeles']}
df = pd.DataFrame(data)
print(df)
```
输出:
```
name age city
0 John 30 New York
1 Jane 25 San Francisco
2 Sam 35 Los Angeles
```
也可以用 numpy ndarray 或 list 来新建 DataFrame
```python
import numpy as np
df = pd.DataFrame(np.random.randn(5,5),columns = ['a','b','c','d','e'])
print(df)
```
输出:
```
a b c d e
0 -1.846199 -0.263414 -1.922507 -0.857989 -0.053102
1 -1.846199 -0.263414 -1.922507 -0.857989 -0.053102
2 -1.846199 -0.263414 -1.922507 -0.857989 -0.053102
3 -1.846199 -0.263414 -1.922507 -0.857989 -0.053102
4 -1.846199 -0.263414 -1.922507 -0.857989 -0.053102
```
阅读全文