这可能是一个简单的问题,但我找不到任何简单的方法。想象一下下面的数据帧:
df = pd.DataFrame(index=range(10), columns=range(5))
以及三个列表,其中包含我要更改的已定义数据帧的索引、列和值:
idx_list = [1,5,3,7] # the indices of the cells that I want to change
col_list = [1,4,3,1] # the columns of the cells that I want to change
value_list = [9,8,7,6] # the final value of whose cells`
我想知道在pandas
中是否有一个函数可以有效地执行以下操作:
for i in range(len(idx_list)):
df.loc[idx_list[i], col_list[i]] = value_list[i]
谢谢。你知道吗
使用
.values
或者另一种效率更低的方法
试试看数据框应用映射()函数,您可以使用lambda来执行所需的操作。你知道吗
相关问题 更多 >
编程相关推荐