gpt4 book ai didi

Python Pandas : Breaking a list or series into columns of different sizes

转载 作者:行者123 更新时间:2023-12-01 09:15:40 25 4
gpt4 key购买 nike

我有一个包含 2 列的系列,看起来像

1   5.3
2 2.5
3 1.6
4 3.8
5 2.8

...等等。我想把这个系列分成 6 个不同大小的栏目。因此(例如)第一列将包含 30 个项目,接下来的 31 个项目,接下来的 28 个项目,依此类推。我见过很多相同大小的列的例子,但还没有看到制作多个自定义大小的列。

最佳答案

根据评论,您可以尝试使用该系列的索引来填充您的数据框

s = pd.Series([5, 2, 1, 3, 2])
df = pd.DataFrame([], index=s.index)
df['col1'] = s.loc[:2]
df['col2'] = s.loc[3:3]
df['col3'] = s.loc[4:]

结果:

    col1  col2  col3
0 5.0 NaN NaN
1 2.0 NaN NaN
2 1.0 NaN NaN
3 NaN 3.0 NaN
4 NaN NaN 4.0

关于Python Pandas : Breaking a list or series into columns of different sizes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291382/

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