向包含父节点符号的数据帧添加列

2024-09-23 16:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是来自CPC website的批量数据(CPC有效符号列表)。我已经将csv读入一个文件,前30行(超过260K)是:

    SYMBOL  level   not-allocatable additional-only
1   A   2   True    False
2   A01 4   True    False
3   A01B    5   True    False
4   A01B 1/00   7   False   False
5   A01B 1/02   8   False   False
6   A01B 1/022  9   False   False
7   A01B 1/024  9   False   False
8   A01B 1/026  9   False   False
9   A01B 1/028  9   False   False
10  A01B 1/04   9   False   False
11  A01B 1/06   8   False   False
12  A01B 1/065  9   False   False
13  A01B 1/08   9   False   False
14  A01B 1/10   9   False   False
15  A01B 1/12   9   False   False
16  A01B 1/14   9   False   False
17  A01B 1/16   8   False   False
18  A01B 1/165  9   False   False
19  A01B 1/18   9   False   False
20  A01B 1/20   8   False   False
21  A01B 1/22   8   False   False
22  A01B 1/222  9   False   False
23  A01B 1/225  10  False   False
24  A01B 1/227  9   False   False
25  A01B 1/24   8   False   False
26  A01B 1/243  9   False   False
27  A01B 1/246  9   False   False
28  A01B 3/00   7   False   False
29  A01B 3/02   8   False   False

level值创建层次结构。因此,节点A01B 1/00是级别7,是A01B的子节点。A01B 1/02是级别8,A01B 1/00和A01B 3/00的子节点是A01B的子节点

我想要的是创建一个名为PARENT的新列,其中包含节点的直接父节点的SYMBOL。例如,我在Excel中编辑了csv以显示前几行的所需结果:

The dataframe after adding the parent column

Note: there are no level 1, 3, or 6 symbols. There are multiple level 2 symbols. There is no parent for level 2 symbols, the parent of level 4 symbols can be assigned the first level 2 symbol above it, and the parent of level 7 symbols likewise can be assigned the first level 5 symbol above it.

编辑:我需要更好地解释如何确定节点的父节点。级别值和行位置是确定父级所需的全部。Tree showing parents and children

我想用熊猫来做这项工作,但我不知道如何开始。有人要吗?谢谢


Tags: csvthenofalsetrue编辑节点symbol
2条回答

在这个答案中,我假设你的直接父母总是排在你自己的上面,因为这是你期望的结果和你的图表所暗示的。你知道吗

根据这个假设,我们可以为每一行取最接近的一行,其级别低于该行:

import pandas as pd

data={"Symbol":["A", "A01", "A01B", "A01B 1/00", "A01B 1/02", "A01B 1/022", "B"], "level":[2,4,5,7,8,9,2]}

df=pd.DataFrame(data=data)
df['Parent'] = ''

for index, row in df.iterrows():
    # We look at the potential parents
    potential_parents = df.loc[df.index.isin([x for x in range(index)]) & (df['level'] < row['level']), 'Symbol']
    # And we take the last one as our parent
    if len(potential_parents) == 0: 
        df.loc[index, 'Parent'] = ''
    else:
        df.loc[index, 'Parent'] = potential_parents.iloc[-1]

输出:

       Symbol  level     Parent
0           A      2           
1         A01      4          A
2        A01B      5        A01
3   A01B 1/00      7       A01B
4   A01B 1/02      8  A01B 1/00
5  A01B 1/022      9  A01B 1/02
6           B      2           

这是另一种方法。GetParent()返回一个函数,该函数跟踪每个级别的最新符号,并返回当前级别的父级。把它用在熊猫。应用()使用父符号创建列。你知道吗

def GetParent():
    #            0  1  2  3  4  5  6  7  8  9  10
    hierarchy = [0, 0, 0, 0, 2, 4, 0, 5, 7, 8, 9]
    parent = [' ']*11

    def func(row):
        #print(row)
        symbol,level = row[['SYMBOL', 'level']]

        parent_level = hierarchy[level]
        parent_symbol = parent[parent_level]

        parent[level] = symbol

        return pd.Series([parent_symbol], index=['parent'])

    return func

# create a column with the parents
parents = df.apply(GetParent(), axis=1)
df = pd.concat([df, parents], axis=1)

df

输出:

    SYMBOL  level   na      ao      parent
0   A           2   True    False   
1   A01         4   True    False   A
2   A01B        5   True    False   A01
3   A01B 1/00   7   False   False   A01B
4   A01B 1/02   8   False   False   A01B 1/00
5   A01B 1/022  9   False   False   A01B 1/02
6   A01B 1/024  9   False   False   A01B 1/02
7   A01B 1/026  9   False   False   A01B 1/02
8   A01B 1/028  9   False   False   A01B 1/02
9   A01B 1/04   9   False   False   A01B 1/02
10  A01B 1/06   8   False   False   A01B 1/00
11  A01B 1/065  9   False   False   A01B 1/06
12  A01B 1/08   9   False   False   A01B 1/06
...

相关问题 更多 >