gpt4 book ai didi

python - 如何填充 Pandas 中的重复值?

转载 作者:行者123 更新时间:2023-11-28 17:55:37 26 4
gpt4 key购买 nike

我有一个具有以下形状的 Pandas 数据框:

>> dataset.shape
(1942,28)

我想创建一个新的数据框 df_new,我在其中获取 dataset 的列名,并使它们在我的 df_new 中重复值.

这是一个例子:

>> dataset.columns
['a', 'b', 'c', 'd']

我希望我的 df_new 看起来像:

    column_name 
0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
8 a
9 b
10 c
11 d
. .
. .
(until the length of the array)

目前,当我编写以下代码时,我没有得到想要的答案。

>> df_new = pd.DataFrame({0:np.arange(0,28).repeat(dataset_ts.shape[1])})
0
0 0
1 0
2 0
. .
. .
. .
27 0
28 1
29 1
30 1
. .
. .

最佳答案

使用numpy.tile :

cols = dataset.columns            
length = dataset_ts.shape[0]

df_new = pd.DataFrame({'new': np.tile(cols, length)})
print (df_new)
new
0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
8 a
9 b
10 c
...
...
...

关于python - 如何填充 Pandas 中的重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58659523/

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