给定数据帧df
:
A B C
0.10 0.83 0.07
0.40 0.30 0.30
0.70 0.17 0.13
0.72 0.04 0.24
0.15 0.07 0.78
以及系列s
:
A 3
B 0
C 4
dtype: int64
有没有办法轻松设置
列A
的3
元素,
列B
&;的0
元素
列C
的4
元素没有在序列上循环?你知道吗
徒劳的东西:
df.loc[s] = 'spam'
(但这会设置整行)
所需输出为:
A B C
0.10 spam 0.07
0.40 0.30 0.30
0.70 0.17 0.13
spam 0.04 0.24
0.15 0.07 spam
有几种方法可以做到这一点。两者都需要将数据转换为
object
类型,以便将字符串分配给以前的float
序列。你知道吗选项1:numpy
这要求您通过整数数组或元组列表输入坐标。你知道吗
方案2:pd.DataFrame.at数据帧你知道吗
一个非矢量化但更直接的解决方案是在} :
for
循环中使用^{我们是上面定义的级数。你知道吗
然后,我们首先在底层数组中将值设置为
np.nan
,然后分配'spam'
。你知道吗输出:
相关问题 更多 >
编程相关推荐