创建一个五行三列的名为 df1 的DataFrame数组,列名为 [states,years,pops],行名['one','two','three','four','five'] 给df1添加新列,列名为 new_add,值为[7,4,5,8,2]
时间: 2023-10-23 18:13:20 浏览: 151
好的,这是您的问题:
创建一个五行三列的名为 df1 的DataFrame数组,列名为 [states,years,pops],行名['one','two','three','four','five'] 给df1添加新列,列名为 new_add,值为[7,4,5,8,2]
代码如下:
``` python
import pandas as pd
import numpy as np
# 创建 DataFrame 数组
df1 = pd.DataFrame(
{'states': ['CA', 'TX', 'NY', 'FL', 'IL'],
'years': [2016, 2016, 2017, 2018, 2017],
'pops': [38332521, 26448193, 19651127, 19552860, 12882135]},
index=['one', 'two', 'three', 'four', 'five'])
# 添加新列
df1['new_add'] = [7, 4, 5, 8, 2]
print(df1)
```
输出如下:
```
states years pops new_add
one CA 2016 38332521 7
two TX 2016 26448193 4
three NY 2017 19651127 5
four FL 2018 19552860 8
five IL 2017 12882135 2
```
阅读全文