Python/Pandas数据框-返回列名 [英] Python/Pandas dataframe - return column name

查看:664
本文介绍了Python/Pandas数据框-返回列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将列的名称/标题返回到pandas数据框中的字符串中?我想使用具有相同前缀的数据行.数据帧头看起来像这样:

Is there a way to return the name/header of a column into a string in a pandas dataframe? I want to work with a row of data which has the same prefix. The dataframe header looks like this:

col_00 | col_01 | ... | col_51 | bc_00 | cd_00 | cd_01 | ... | cd_90

我想对每行应用一个函数,但只能分别从col_00col_51以及从cd_00cd_90.为此,我想我会将列名称收集到一个列表中,例如fe. to_work_with将是以前缀'col'开头的列的列表,将该功能应用于df[to_work_with].然后,我将更改to_work_with,它将包含以'cd'前缀等开头的列的列表.但是我不知道如何遍历列名.

I'd like to apply a function to each row, but only from col_00 to col_51 and to cd_00 to cd_90 separately. To do this, I thought I'd collect the column names into a list, fe. to_work_with would be the list of columns starting with the prefix 'col', apply the function to df[to_work_with]. Then I'd change the to_work_with and it would contain the list of columns starting with the 'cd' prefix et cetera. But I don't know how to iterate through the column names.

基本上,我要寻找的是此功能:

So basically, the thing I'm looking for is this function:

to_work_with = column names in the df that start with "thisstring"

我该怎么做?谢谢!

推荐答案

您可以使用 boolean indexing

You can use boolean indexing with str.startswith:

cols = df.columns[df.columns.str.startswith('cd')]
print (cols)
Index(['cd_00', 'cd_01', 'cd_02', 'cd_90'], dtype='object')

示例:

print (df)
   col_00  col_01  col_02  col_51  bc_00  cd_00  cd_01  cd_02  cd_90
0       1       2       3       4      5      6      7      8      9

cols = df.columns[df.columns.str.startswith('cd')]
print (cols)
Index(['cd_00', 'cd_01', 'cd_02', 'cd_90'], dtype='object')

#if want apply some function for filtered columns only
def f(x):
    return x + 1

df[cols] = df[cols].apply(f)    
print (df)
   col_00  col_01  col_02  col_51  bc_00  cd_00  cd_01  cd_02  cd_90
0       1       2       3       4      5      7      8      9     10

使用list comprehension的另一种解决方案:

Another solution with list comprehension:

cols = [col for col in df.columns if col.startswith("cd")]
print (cols)
['cd_00', 'cd_01', 'cd_02', 'cd_90']

这篇关于Python/Pandas数据框-返回列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆