use*_*143 73 python dataframe pandas
有没有办法在pandas DataFrame对象中选择除一列之外的所有列?我已经看到了删除列的方法,但我不想这样做.
HYR*_*YRY 103
使用drop方法:
df.drop(column_name, axis=1)
Run Code Online (Sandbox Code Playgroud)
EdC*_*ica 15
您只需选择所需的列而不删除或删除:
collist = ['col1', 'col2', 'col3']
df1 = df[collist]
Run Code Online (Sandbox Code Playgroud)
只需传递所需列的列表即可
您还可以检索列列表,然后从该列表中进行选择
collist = df.columns.tolist()
# you can now select from this list any arbritrary range
df1 = df[collist[0:1]]
# or remove a column
collist.remove('col2')
# now select
df1 = df[collist]
# df1 will now only have 'col1' and 'col3'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50020 次 |
| 最近记录: |