【发布时间】:2021-12-14 16:59:34
【问题描述】:
df1 =
| name | age | branch | subject | date of joining | |
|---|---|---|---|---|---|
| 1 | Steve | 27 | Mechanical | Autocad | 01-08-2021 |
| 2 | Adam | 32 | Electrical | control sys | 14-08-2021 |
| 3 | Raj | 24 | Electrical | circuit | 20-08-2021 |
| 4 | Tim | 25 | Computers | clouding | 21-08-2021 |
df2= [['name','branch']]
打印(df2)
| name | branch | |
|---|---|---|
| 1 | Steve | Mechanical |
| 2 | Adam | Electrical |
| 3 | Raj | Electrical |
| 4 | Tim | Computers |
现在我有两个数据框,
我只需要 name 和 branch 列并删除剩余的列,所有这些操作都应该适用于原始 df1。我不想单独df2
【问题讨论】:
-
我不明白这个问题 - 你的意思是你想从 df1 中提取
name和branch列来制作 df2? -
简单,只覆盖 df1,df1= df1[['name','branch']] 或 del df1(创建 df2 之后)
-
@Bashton 是的。我只需要 name 和 branch 并删除其余列,所有这些操作都应该应用于原始 df1。
-
@Rajeev 如何在创建 d2 后删除 df1?
-
不会影响,是你的本地系统(你没有运行任何数据库查询)
标签: python python-3.x pandas dataframe overwrite