pandas编程一个表有三列在原表中删除列名中包含O的所有列能否用filter
方法实现?
可以使用filter()方法实现。具体步骤如下:
- 获取原表的列名列表。
- 使用filter()方法过滤出不包含'O'的列名列表。
- 使用原表的loc[]方法,根据过滤后的列名列表,获取新表。
示例代码如下:
import pandas as pd
# 创建示例数据表
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# 获取列名列表
cols = df.columns.tolist()
# 过滤列名列表
filtered_cols = list(filter(lambda x: 'O' not in x, cols))
# 获取新表
new_df = df.loc[:, filtered_cols]
# 输出结果
print(new_df)
输出结果为:
A C
0 1 7
1 2 8
2 3 9
可以看到,新表中已经删除了列名中包含'O'的列。
原文地址: https://www.cveoy.top/t/topic/cpWm 著作权归作者所有。请勿转载和采集!