stock方法?

2wnc66cl  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(300)

在这两种形式之间转换有好的现有方法吗?

+--------+-------------+
| FRUIT  | ATTRIBUTES  |
+--------+-------------+
| banana | long|yellow |
+--------+-------------+
| kiwi   | brown|oval  |
+--------+-------------+

+--------+-----------+
| FRUIT  | ATTRIBUTE |
+--------+-----------+
| banana | long      |
+--------+-----------+
| banana | yellow    |
+--------+-----------+
| kiwi   | brown     |
+--------+-----------+
| kiwi   | oval      |
+--------+-----------+

我目前正在迭代解包过程的行,我听说不鼓励这样做。

import pandas as pd

packed = pd.DataFrame([['banana', 'long|yellow'], 
                       ['kiwi', 'brown|oval']], 
                      columns=['FRUIT', 'ATTRIBUTES'])

pack_delim = '|'

per_fruit_frames = []

for row in packed.itertuples(index=True, name='Pandas'):
    row_attribs = row.ATTRIBUTES
    row_attribs_split = row_attribs.split(pack_delim)
    row_attribs_series = pd.Series(row_attribs_split)
    ras_len = len(row_attribs_split)
    fruit_rep = [row[1]] * ras_len
    frs = pd.Series(fruit_rep)
    temp = pd.concat([frs, row_attribs_series], axis=1)
    per_fruit_frames.append(temp)

unpacked = pd.concat(per_fruit_frames)

unpacked.columns = packed.columns
ffdz8vbo

ffdz8vbo1#

尝试:

packed=(packed.assign(ATTRIBUTES=packed['ATTRIBUTES'].str.split('|'))
              .explode('ATTRIBUTES',ignore_index=True))


分两步进行:

packed['ATTRIBUTES']=packed['ATTRIBUTES'].str.split('|')
packed=packed.explode('ATTRIBUTES',ignore_index=True)

产量 packed :

FRUIT   ATTRIBUTES
0   banana  long
1   banana  yellow
2   kiwi    brown
3   kiwi    oval
vsdwdz23

vsdwdz232#

另一种方式:

>>> pd.merge(packed['FRUIT'], packed['ATTRIBUTES'].str.split('|').explode(),
             left_index=True, right_index=True)

    FRUIT ATTRIBUTES
0  banana       long
0  banana     yellow
1    kiwi      brown
1    kiwi       oval

相关问题