根据条件重命名 pandas 数据框的多列 [英] rename multiple columns of pandas dataframe based on condition
问题描述
我有一个df,其中我需要将40个列名重命名为空字符串.这可以通过使用.rename()
来实现,但是我需要在dict中提供所有列名,而这些列名需要重命名.我正在寻找通过模式匹配来重命名列的更好方法.凡在列名中找到NULL/UNNAMED的地方,都用空字符串替换.
I have a df in which I need to rename 40 column names to empty string. this can be achieved by using .rename()
, but I need to provide all the column names in dict, which needs to be renamed. I was searching for some better way to rename columns by some pattern matching. wherever it finds NULL/UNNAMED in column name, replace that with empty string.
df1:原始df(在实际df中,我大约有20列为NULL1-NULL20,还有20列为UNNAMED1-UNNAMED20)
df1: original df (In actual df, i have around 20 columns as NULL1-NULL20 and 20 columns as UNNAMED1-UNNAMED20)
NULL1 NULL2 C1 C2 UNNAMED1 UNNAMED2
0 1 11 21 31 41 51
1 2 22 22 32 42 52
2 3 33 23 33 43 53
3 4 44 24 34 44 54
所需的输出df:
C1 C2
0 1 11 21 31 41 51
1 2 22 22 32 42 52
2 3 33 23 33 43 53
3 4 44 24 34 44 54
这可以通过
df.rename(columns={'NULL1':'', 'NULL2':'', 'UNNAMED1':'', 'UNNAMED2':''}, inplace=True)
但是我不想创建40个元素的长字典
But I dont want to create the long dictionary of 40 elements
推荐答案
如果您要坚持使用rename
:
def renaming_fun(x):
if "NULL" in x or "UNNAMED" in x:
return "" # or None
return x
df = df.rename(columns=renaming_fun)
如果映射功能变得更复杂,可能会很方便.否则,列表理解将起作用:
It can be handy if the mapping function gets more complex. Otherwise, list comprehensions will do:
df.columns = [renaming_fun(col) for col in cols]
另一种可能性:
df.columns = map(renaming_fun, df.columns)
但是正如已经提到的,用空字符串重命名通常不是您要做的事情.
But as it was already mentioned, renaming with empty strings is not something you would usually do.
这篇关于根据条件重命名 pandas 数据框的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!