gpt4 book ai didi

python - 将重复值插入 Pandas 行

转载 作者:行者123 更新时间:2023-11-30 22:32:51 26 4
gpt4 key购买 nike

我想拆分乔布斯,史蒂夫。 01/31 列,以便 [SPGC-9456, 6.0]] 位于其自己的行上。

我的代码现在输出什么:

                                             2017-01-31           2017-02-01
Gates, Bill. [[SPGC-14075, 0.5]] NaN
Jobs, Steve. [[SPGC-14075, 3.5], [SPGC-9456, 6.0]] NaN
White, John ANDERSON. [[SPGC-14075, 1.75]] [[SPGC-9456, 1.75]]

我想要什么:

                                            2017-01-31           2017-02-01
Gates, Bill. [[SPGC-14075, 0.5]] NaN
Jobs, Steve. [[SPGC-14075, 3.5] NaN
Jobs, Steve. [SPGC-9456, 6.0]] NaN
White, John ANDERSON. [[SPGC-14075, 1.75]] [[SPGC-9456, 1.75]]

最佳答案

col = '2017-01-31'
v = df[col].values.tolist()
l = [len(x) for x in v]
d = {col: [[x] for y in v for x in y]}
df.reindex(df.index.repeat(l)).assign(**d)

2017-01-31 2017-02-01
Gates, Bill. [[SPGC-14075, 0.5]] NaN
Jobs, Steve. [[SPGC-14075, 3.5]] NaN
Jobs, Steve. [[SPGC-9456, 6.0]] NaN
White, John ANDERSON. [[SPGC-14075, 1.75]] [[SPGC-9456, 1.75]]
<小时/>

设置

df = pd.DataFrame([
[[['SPGC-14075', .5]], np.nan],
[[['SPGC-14075', 3.5], ['SPGC-9456', 6.]], np.nan],
[[['SPGC-14075', 1.75]], [['SPGC-9456', 1.75]]]
],
'Gates, Bill.|Jobs, Steve.|White, John ANDERSON.'.split('|'),
['2017-01-31', '2017-02-01']
)

关于python - 将重复值插入 Pandas 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com