为特定列添加前缀 Dataframe
我有 DataFrame 这样的 :
我想重命名所有列,但不是
col1
和
col2
.
所以我试图制作一个循环
但这不是很好 efficient...it 不起作用 !
col1 col2 col3 col4 col5 col6 col7 col8
0 5345 rrf rrf rrf rrf rrf rrf
1 2527 erfr erfr erfr erfr erfr erfr
2 2727 f f f f f f
我想重命名所有列,但不是
col1
和
col2
.
所以我试图制作一个循环
print/df.columns/
for col in df.columns:
if col != 'col1' and col != 'col2':
col.rename = str/col/ + '_x'
但这不是很好 efficient...it 不起作用 !
没有找到相关结果
已邀请:
3 个回复
詹大官人
赞同来自:
知食
赞同来自:
和
- 这些是第一个和第二列的名称:
另一个决定。
http://pandas.pydata.org/panda ... .html
或理解列表:
最快的是对列表的理解:
df.columns = [col+'_x' if col != 'col1' and col != 'col2' else col for col in df.columns]
Tayyumi
:
帅驴
赞同来自:
http://pandas.pydata.org/panda ... .html
有模板 regex 用于过滤感兴趣 cols, 然后在帮助下
建造 dict 并将其转移为 arg 在
http://pandas.pydata.org/panda ... .html
:
所以这是使用
过滤列将返回不重合的列,因此列的顺序无关紧要