【发布时间】:2021-04-01 22:55:55
【问题描述】:
我有一个 CSV 文件,例如:
| id | name | physics | chemistry | maths | |
|---|---|---|---|---|---|
| 1 | Sta | sta@example.com | 67 | 78 | 90 |
| 2 | Danny | dany@example.com | 77 | 98 | 89 |
| 3 | Elle | elle@example.com | 77 | 67 | 90 |
现在我想使用具有新列的 pandas 输出一个新的 CSV 文件,例如:
| id | name | grade | address | physics | chemistry | attendance | maths | total |
|---|
我想在随机位置创建新列,我想在新列中将值设置为空白。
我尝试过使用:
import pandas as pd
df = pd.read_csv("sample.csv")
final_df = df.loc[['id','name','grade','address','physics','chemistry','attendance','maths','total']]
当我这样做时,我得到了一个错误:
KeyError(f“None of [{key}] are in the [{axis_name}]”)
任何安排这个的想法或建议。
【问题讨论】:
标签: python python-3.x pandas dataframe numpy