gpt4 book ai didi

python - pandas.DataFrame 中重复列的有趣结果

转载 作者:行者123 更新时间:2023-12-03 09:38:18 24 4
gpt4 key购买 nike

pandas.DataFrame 中存在重复列时,任何人都可以帮助解释为什么我在某些操作中出错而不是在其他操作中出错.
最小的、可重现的示例

import pandas as pd
df = pd.DataFrame(columns=['a', 'b', 'b'])
如果我尝试将列表插入 column 'a'我收到关于尺寸不匹配的错误:
df.loc[:, 'a'] = list(range(5))

Traceback (most recent call last):
...
ValueError: cannot copy sequence with size 5 to array axis with dimension 0
类似 'b' :
df.loc[:, 'b'] = list(range(5))

Traceback (most recent call last):
...
ValueError: could not broadcast input array from shape (5) into shape (0,2)
但是,如果我插入到一个全新的列中,则不会出现错误,除非我插入到 'a' 中。或 'b' :
df.loc[:, 'c'] = list(range(5))
print(df)

a b b c
0 NaN NaN NaN 0
1 NaN NaN NaN 1
2 NaN NaN NaN 2
3 NaN NaN NaN 3
4 NaN NaN NaN 4

df.loc[:, 'a'] = list(range(5))

Traceback (most recent call last):
...
ValueError: Buffer has wrong number of dimensions (expected 1, got 0)
如果我删除重复的列 'b',所有这些错误都会消失。

附加信息 pandas==1.0.2

最佳答案

为什么使用 loc 而不仅仅是:

df['a'] = list(range(5))
这不会出错,并且似乎可以产生您需要的内容:
a   b   b
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
创建列 c 相同:
df['c'] = list(range(5))

关于python - pandas.DataFrame 中重复列的有趣结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65255166/

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