gpt4 book ai didi

python - 同时列分配

转载 作者:行者123 更新时间:2023-12-01 04:51:10 25 4
gpt4 key购买 nike

我刚刚发现我不能这样分配:

df[['a','b','c']] = 'total'

df.loc[:, ['a','b','c']] = 'total

但是,我可以

df['a'] = 'total'
df['b'] = 'total'
df['c'] = 'total'

df['a'] = df['b'] = df['c'] = 'total'

我错过了什么吗?

最佳答案

您可以使用ix同时设置多个列,不会出现问题:

In [8]:

df = pd.DataFrame({'a':np.random.randn(5), 'b':np.random.randn(5), 'c':np.random.randn(5)})
df
Out[8]:
a b c
0 0.623686 0.875752 1.027399
1 -0.806096 0.349891 0.290276
2 0.750623 0.704666 -1.401591
3 -0.594068 -1.148006 0.373021
4 -1.060019 -0.325563 -0.536769

In [9]:

df.ix[:,['a','b','c']] = 'total'
df
Out[9]:
a b c
0 total total total
1 total total total
2 total total total
3 total total total
4 total total total

关于python - 同时列分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444511/

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