gpt4 book ai didi

python - 将列添加到 Pandas 数据框时如何避免列和 DatetimeIndex 之间的混淆

转载 作者:太空宇宙 更新时间:2023-11-03 23:52:36 24 4
gpt4 key购买 nike

我有一个 pandas DataFrame,其列标题是数字字符串,索引是 DatetimeIndex。例如:

In:
df=pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]], index=pd.DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 00:05:00',
'2019-01-01 00:10:00']), columns=['010000','010001','010002'])
df

Out:
010000 010001 010002
2019-01-01 00:00:00 1 2 3
2019-01-01 00:05:00 4 5 6
2019-01-01 00:10:00 7 8 9

我成功地将列添加到数据框中,例如,

In:
df['010003'] = pd.Series([99,99,99], index= df.index)
df
Out:
010000 010001 010002 010003
2019-01-01 00:00:00 1 2 3 99
2019-01-01 00:05:00 4 5 6 99
2019-01-01 00:10:00 7 8 9 99

但是,如果列标题可能被误认为是日期,Pandas 会将其视为索引元素,尝试添加行而不是列,并引发异常:

In:
df['010119'] = pd.Series([99,99,99], index= df.index)
Out:
Traceback (most recent call last):
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-16-1f55509f2987>", line 1, in <module>
df['010119'] = pd.Series([99,99,99], index= df.index)
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 3362, in __setitem__
return self._setitem_slice(indexer, value)
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 3374, in _setitem_slice
self.loc._setitem_with_indexer(key, value)
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 656, in _setitem_with_indexer
value=value)
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 510, in setitem
return self.apply('setitem', **kwargs)
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 395, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\blocks.py", line 920, in setitem
values[indexer] = value
ValueError: could not broadcast input array from shape (3) into shape (3,4)

为避免这种混淆,我应该如何重写赋值以强制 Pandas 将数字字符串作为新列的标题?

最佳答案

我们有插入列的特定功能

df.insert(len(df.columns),column='010119',value=[99,99,99])
df
010000 010001 010002 010119
2019-01-01 00:00:00 1 2 3 99
2019-01-01 00:05:00 4 5 6 99
2019-01-01 00:10:00 7 8 9 99

关于python - 将列添加到 Pandas 数据框时如何避免列和 DatetimeIndex 之间的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847955/

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