要删除pandas DataFrame中的最后一列,可以使用drop函数。

下面是一个示例代码:

import pandas as pd

# 创建一个DataFrame
data = {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
df = pd.DataFrame(data)

# 删除最后一列
df = df.drop(df.columns[-1], axis=1)

print(df)

输出结果为:

   col1  col2
0     1     4
1     2     5
2     3     6

drop函数中,df.columns[-1]表示最后一列的列名,axis=1表示删除列。最后将删除后的DataFrame赋值给原始DataFrame,即可删除最后一列。

python的pandas删除最后一列的代码

原文地址: http://www.cveoy.top/t/topic/iYxe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录