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']]
print(df2)
name | branch | |
---|---|---|
1 | Steve | Mechanical |
2 | Adam | Electrical |
3 | Raj | Electrical |
4 | Tim | Computers |
Now I have two data frames,
I need only name and branch columns and remove the remaining columns, all these operations should apply to the original df1. I don't want separately df2
CodePudding user response:
Simply, Overwrite the df1 only
df1= df1[['name','branch']] or df2= df1[['name','branch']] del df1
To delete df1 or df2. del df1 or del df2
Based on requirement
CodePudding user response:
You can simply set df1
to df2
or you can drop the columns from df1
.
Method 1: drop in place
columns_to_drop = [x for x in df1.columns if x not in ['name','branch']]
df1.drop(columns=columns_to_drop, inplace=True)
Method 2:
df1 = df1[['name','branch']]