gpt4 book ai didi

python - 为所有行创建具有相同元组值的列

转载 作者:行者123 更新时间:2023-11-28 17:02:58 25 4
gpt4 key购买 nike

我想创建一个包含相同值的列。我希望该值是一个元组。不幸的是,pandas 认为我正在尝试传递一整列值。

df = pd.DataFrame(index=range(10))
df['foo']=9 #ok
df['bar']=(10,12) #think's I'm passing in a too-short column

ValueError: Length of values does not match length of index

如何将列“bar”的所有行设置为元组?

最佳答案

您可以使用 DataFrame 构造函数:

df = pd.DataFrame({'foo': 9, 'bar':[(10,12)]}, index=range(10))

或使用列表推导式或按 DataFrame 的长度重复元组:

df = pd.DataFrame(index=range(10))
df['foo']=9 #ok
df['bar']= [(10,12) for _ in df.index]
#another solution
#df['bar']= [(10,12)] * len(df)

print (df)
foo bar
0 9 (10, 12)
1 9 (10, 12)
2 9 (10, 12)
3 9 (10, 12)
4 9 (10, 12)
5 9 (10, 12)
6 9 (10, 12)
7 9 (10, 12)
8 9 (10, 12)
9 9 (10, 12)

性能:

df = pd.DataFrame(index=range(1000))

In [99]: %%timeit
...: df['bar']='10,12'
...: df['bar']=df['bar'].str.split(',').astype(tuple)
...:
977 µs ± 37.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [100]: %%timeit
...: df['bar']= [(10,12) for _ in df.index]
...:
218 µs ± 3.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [101]: %%timeit
...: df['bar']= [(10,12)] * len(df)
...:
175 µs ± 8.46 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [105]: %%timeit
...: df = pd.DataFrame({'foo': 9, 'bar':[(10,12)]}, index=range(1000))
...:
400 µs ± 1.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [106]: %%timeit
...: df = pd.DataFrame(index=range(1000))
...: df['foo']=9
...: df['bar']= [(10,12)] * len(df)
...:
766 µs ± 5.11 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

关于python - 为所有行创建具有相同元组值的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53184989/

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