如果col1和col2是第一和第二列名,则最简单的解决方案是:df.columns = df.columns[:2].union(df.columns[2:] + ‘_x’)
print (df)
col1 col2 col3_x col4_x col5_x col6_x col7_x col8_x
0 0 5345 rrf rrf rrf rrf rrf rrf
1 1 2527 erfr erfr erfr erfr erfr erfr
2 2 2727 f f f f f f
另一个有^{}或列表理解的解决方案:cols = df.columns[~df.columns.isin([‘col1′,’col2’])]
print (cols)
[‘col3’, ‘col4’, ‘col5’, ‘col6’, ‘col7’, ‘col8’]
df.rename(columns = dict(zip(cols, cols + ‘_x’)), inplace=True)
print (df)
col1 col2 col3_x col4_x col5_x col6_x col7_x col8_x
0 0 5345 rrf rrf rrf rrf rrf rrf
1 1 2527 erfr erfr erfr erfr erfr erfr
2 2 2727 f f f f f fcols = [col for col in df.columns if col not in [‘col1’, ‘col2’]]
print (cols)
Original: https://blog.csdn.net/weixin_34836906/article/details/113674574
Author: 栩栩少根筋
Title: python给一列数据增加前缀_为Datafram的特定列添加前缀
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/756044/
转载文章受原作者版权保护。转载请注明原作者出处!