Python Pandas-将列表转换为系列

时间:2019-11-20 06:07:05

标签: python pandas

我有一个如下所示的excel数据集:

.class_indices

出于复制目的:

ID  buffer
LocalHub@3c183d50   [intraCity_Simulator.Parcel@55078545, intraCity_Simulator.Parcel@75b895dd, intraCity_Simulator.Parcel@44227899, intraCity_Simulator.Parcel@696b0129, intraCity_Simulator.Parcel@86ec871, intraCity_Simulator.Parcel@7a0d8542, intraCity_Simulator.Parcel@67a58fba]
LocalHub@d3a0fbe    [intraCity_Simulator.Parcel@61b9a28c, intraCity_Simulator.Parcel@1b5d2e8b, intraCity_Simulator.Parcel@65911201, intraCity_Simulator.Parcel@2e53ab95, intraCity_Simulator.Parcel@464b73fa, intraCity_Simulator.Parcel@640ff28a, intraCity_Simulator.Parcel@77fc8d6c, intraCity_Simulator.Parcel@609051b0, intraCity_Simulator.Parcel@25e0c299, intraCity_Simulator.Parcel@436af74b, intraCity_Simulator.Parcel@24c3fb2, intraCity_Simulator.Parcel@130592c8, intraCity_Simulator.Parcel@444d20b1, intraCity_Simulator.Parcel@6d59d5b2, intraCity_Simulator.Parcel@764a25d3, intraCity_Simulator.Parcel@4bdd2c62]

我想重新排列列表值并将其显示为与ID对应的列,例如

ID                        buffer
LocalHub@3c183d50       intraCity_Simulator.Parcel@55078545
LocalHub@3c183d50       intraCity_Simulator.Parcel@75b895dd
...                     ...

1 个答案:

答案 0 :(得分:1)

Series.str.strip用列表的Series.str.splitDataFrame.explode除去[],然后DataFrame.reset_indexdrop=True除去Series.str.len默认为RangeIndex

df = (df.assign(buffer = df['buffer'].str.strip('[]').str.split(','))
        .explode('buffer')
        .reset_index(drop=True))
print (df)

                   ID                                buffer
0   LocalHub@3c183d50   intraCity_Simulator.Parcel@55078545
1   LocalHub@3c183d50   intraCity_Simulator.Parcel@75b895dd
2   LocalHub@3c183d50   intraCity_Simulator.Parcel@44227899
3   LocalHub@3c183d50   intraCity_Simulator.Parcel@696b0129
4   LocalHub@3c183d50    intraCity_Simulator.Parcel@86ec871
5   LocalHub@3c183d50   intraCity_Simulator.Parcel@7a0d8542
6   LocalHub@3c183d50   intraCity_Simulator.Parcel@67a58fba
7    LocalHub@d3a0fbe    inraCity_Simulator.Parcel@61b9a28c
8    LocalHub@d3a0fbe   intraCity_Simulator.Parcel@1b5d2e8b
9    LocalHub@d3a0fbe   intraCity_Simulator.Parcel@65911201
10   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@2e53ab95
11   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@464b73fa
12   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@640ff28a
13   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@77fc8d6c
14   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@609051b0
15   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@25e0c299
16   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@436af74b
17   LocalHub@d3a0fbe    intraCity_Simulator.Parcel@24c3fb2
18   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@130592c8
19   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@444d20b1
20   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@6d59d5b2
21   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@764a25d3
22   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@4bdd2c62

以下熊猫版本的解决方案是按{{3}}的列表长度使用repeat

from itertools import chain

splitted = df['buffer'].str.strip('[]').str.split(',')
df = pd.DataFrame({
    'ID' : df['ID'].values.repeat(splitted.str.len()),
    'buffer' : list(chain.from_iterable(splitted.tolist()))
})

print (df)
                   ID                                buffer
0   LocalHub@3c183d50   intraCity_Simulator.Parcel@55078545
1   LocalHub@3c183d50   intraCity_Simulator.Parcel@75b895dd
2   LocalHub@3c183d50   intraCity_Simulator.Parcel@44227899
3   LocalHub@3c183d50   intraCity_Simulator.Parcel@696b0129
4   LocalHub@3c183d50    intraCity_Simulator.Parcel@86ec871
5   LocalHub@3c183d50   intraCity_Simulator.Parcel@7a0d8542
6   LocalHub@3c183d50   intraCity_Simulator.Parcel@67a58fba
7    LocalHub@d3a0fbe    inraCity_Simulator.Parcel@61b9a28c
8    LocalHub@d3a0fbe   intraCity_Simulator.Parcel@1b5d2e8b
9    LocalHub@d3a0fbe   intraCity_Simulator.Parcel@65911201
10   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@2e53ab95
11   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@464b73fa
12   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@640ff28a
13   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@77fc8d6c
14   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@609051b0
15   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@25e0c299
16   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@436af74b
17   LocalHub@d3a0fbe    intraCity_Simulator.Parcel@24c3fb2
18   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@130592c8
19   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@444d20b1
20   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@6d59d5b2
21   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@764a25d3
22   LocalHub@d3a0fbe   intraCity_Simulator.Parcel@4bdd2c62