gpt4 book ai didi

python - 我想分解一个 numpy 数组并将值多次应用于单列

转载 作者:行者123 更新时间:2023-12-01 07:58:28 24 4
gpt4 key购买 nike

我想分解 numpy.ndarray 并将数组的每个值应用于数据帧的第一个元素。这是输入内容

Here is my dataframe

Id Dept

100 Healthcare


Here is my numpy.ndarray

array(['2007-01-03', '2007-01-10', '2007-01-17', '2007-01-24'], dtype='datetime64[D]')


The Output looks like
Id Dept Date

100 Healthcare 2007-01-03
100 Healthcare 2007-01-10
100 Healthcare 2007-01-17
100 Healthcare 2007-01-24


我需要帮助来实现这个逻辑。

最佳答案

您可以使用pandas.concat并使用数组的长度:

x = np.array(['2007-01-03', '2007-01-10', '2007-01-17', '2007-01-24'], dtype='datetime64[D]')

df = pd.concat([df]*len(x))
df['Date'] = x

print(df)
Id Dept Date
0 100 Healthcare 2007-01-03
0 100 Healthcare 2007-01-10
0 100 Healthcare 2007-01-17
0 100 Healthcare 2007-01-24

关于python - 我想分解一个 numpy 数组并将值多次应用于单列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833078/

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