【发布时间】:2019-08-01 22:16:36
【问题描述】:
我正在尝试用另一列替换一列中的值列表,下面是我正在使用的数据和脚本
old = [[51, 1],
[52, 1],
[53, -1],
[],
[54, 0],
[55, 0],
[52, 0],
[],
[52, 0],
[54, -1]]
import pandas as pd
df = pd.DataFrame(columns=['old','new'])
df["old"]=old
new = [[51, 1],
[52, 1],
[53, -1],
[54, -2],
[54, 0],
[55, 0],
[52, 0],
[55. -3],
[52, 0],
[54, -1]]
df["new"]=new
for index, row in df.iterrows():
if ((df["old"][index])==[]) :
df.old[index] = df.new[index]
有没有比使用 for 循环更好的方法,实际上在我的脚本中有太多循环,所以我想避免使用 for 循环进行少量数据操作 如果有人知道这将是很大的帮助
【问题讨论】:
标签: python dataframe data-manipulation