如果相似并处理,则检测 pandas 列名称 [英] detect pandas column names if similiar match and process

查看:74
本文介绍了如果相似并处理,则检测 pandas 列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有导入到Pandas数据框中的数据,在该数据框中,作为列表的元素会自动分离到新的列中.我的数据最初是 .root 文件,我正在使用 Uproot

I have data that gets imported into a Pandas dataframe where that elements that are lists are being separated into new columns automatically. My data was originally .root files and I am importing them in to Pandas with Uproot

下面是示例数据,其中物理列[0]和物理 2 最初是列表元素

Below is example data where the column physics[0] and physics2 were originally elements of a list

data = {'physics[0]': [1,2,3], 'physics[1]': [4,5,6], 'yes': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)



   physics[0]  physics[1]  yes  no 
0           1           4    7  10  
1           2           5    8  11  
2           3           6    9  12  

我试图提出一种技术来检测相似的列名并重新创建元素作为列表.这是我到目前为止的内容:

I am trying to come up with a technique to detect similar column names and recreate the elements as list. Here is what I have so far:

lst = [col for col in df.columns if 'physics' in col]

df['physics']=df[lst].values.tolist()

    yes  no physics
0    7  10  [1, 4]
1    8  11  [2, 5]
2    9  12  [3, 6]

有效.我不会总是事先知道发生这种情况时列名将是什么.但我希望能够自动检测名称是否相似,并执行上面的列表理解.

which works. I won't always know before hand what the column names will be when this happens. But I'd like to be able to detect if the names are similar automatically and perform the above list comprehension.

推荐答案

您可以使用正则表达式来概括您的方法:

You can generalize your approach using regex:

import re
# create dictionary d of all groups of similar columns
multi_cols = filter(lambda x: re.search(r'\[[0-9]+\]$',x),df.columns)
d = {}
for c in multi_cols:
    k = re.sub(r'\[[0-9]+\]$', '' , str(c))
    if k not in d:
        d[k] = []
    d[k].append(c)

# the dictionary will be as following:
print(d)
# {'physics': ['physics[0]', 'physics[1]']}

# use dictionary d to combine all similar columns in each group
for k in d:
    df[k] = df[d[k]].values.tolist()

这篇关于如果相似并处理,则检测 pandas 列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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