如何从列表中为数据框的特定列名附加后缀
How to append a suffix for specific column names of a dataframe from a list
我想根据 df2 中的名称为 df1 的某些列名称附加后缀或前缀。
我的 df1 看起来像这样,
AE02 AE03 AE04 AE05 AE06 AE07 AE08 AE09 AE10 AE11 AE12
11.9619362364 18.5701402709 42.2010838789 28.0025053738 19.5589170223 18.1459582989 16.5292369479 32.4885640738 34.0342144643 31.6971000153 44.932255488
2.9904840591 3.9793157723 0 0 1.7780833657 1.7281865047 13.7743641233 4.3318085432 0 17.067669239 0
0 0 0 0 2.6671250485 0 4.5914547078 0 0 0 2.1396312137
df2 看起来像,
V1
AE06
AE08
AE09
AE12
我可以用新名称替换那些列名称,如下所示,
colnames(df1)[which(colnames(df1) %in% df2$V1 )] <- "DMR"
但我正在寻找一种解决方案,我可以将其作为前缀附加到列名而不是替换它,
例如,我的列名应该是这样的,
AE02 AE03 AE04 AE05 DMR_AE06 AE07 DMR_AE08 DMR_AE09 AE10 AE11 DMR_AE12
非常感谢任何建议和帮助
Pandas解法:
您可以使用 numpy.where
with mask
by Index.isin
:
print (df.columns.isin(df2.V1))
[False False False False True False True True False False True]
df.columns = np.where(df.columns.isin(df2.V1), 'DMR_' + df.columns, df.columns)
print (df)
AE02 AE03 AE04 AE05 DMR_AE06 AE07 \
0 11.961936 18.570140 42.201084 28.002505 19.558917 18.145958
1 2.990484 3.979316 0.000000 0.000000 1.778083 1.728187
2 0.000000 0.000000 0.000000 0.000000 2.667125 0.000000
DMR_AE08 DMR_AE09 AE10 AE11 DMR_AE12
0 16.529237 32.488564 34.034214 31.697100 44.932255
1 13.774364 4.331809 0.000000 17.067669 0.000000
2 4.591455 0.000000 0.000000 0.000000 2.139631
Python 中的列表理解:
df.columns = ['DMR_{}'.format(x) if x in df2.V1.values else x for x in df.columns]
我想根据 df2 中的名称为 df1 的某些列名称附加后缀或前缀。 我的 df1 看起来像这样,
AE02 AE03 AE04 AE05 AE06 AE07 AE08 AE09 AE10 AE11 AE12
11.9619362364 18.5701402709 42.2010838789 28.0025053738 19.5589170223 18.1459582989 16.5292369479 32.4885640738 34.0342144643 31.6971000153 44.932255488
2.9904840591 3.9793157723 0 0 1.7780833657 1.7281865047 13.7743641233 4.3318085432 0 17.067669239 0
0 0 0 0 2.6671250485 0 4.5914547078 0 0 0 2.1396312137
df2 看起来像,
V1
AE06
AE08
AE09
AE12
我可以用新名称替换那些列名称,如下所示,
colnames(df1)[which(colnames(df1) %in% df2$V1 )] <- "DMR"
但我正在寻找一种解决方案,我可以将其作为前缀附加到列名而不是替换它, 例如,我的列名应该是这样的,
AE02 AE03 AE04 AE05 DMR_AE06 AE07 DMR_AE08 DMR_AE09 AE10 AE11 DMR_AE12
非常感谢任何建议和帮助
Pandas解法:
您可以使用 numpy.where
with mask
by Index.isin
:
print (df.columns.isin(df2.V1))
[False False False False True False True True False False True]
df.columns = np.where(df.columns.isin(df2.V1), 'DMR_' + df.columns, df.columns)
print (df)
AE02 AE03 AE04 AE05 DMR_AE06 AE07 \
0 11.961936 18.570140 42.201084 28.002505 19.558917 18.145958
1 2.990484 3.979316 0.000000 0.000000 1.778083 1.728187
2 0.000000 0.000000 0.000000 0.000000 2.667125 0.000000
DMR_AE08 DMR_AE09 AE10 AE11 DMR_AE12
0 16.529237 32.488564 34.034214 31.697100 44.932255
1 13.774364 4.331809 0.000000 17.067669 0.000000
2 4.591455 0.000000 0.000000 0.000000 2.139631
Python 中的列表理解:
df.columns = ['DMR_{}'.format(x) if x in df2.V1.values else x for x in df.columns]