[英]Add Pandas column values in a for loop?
I have the following Python code (not 100% complete): 我有以下Python代码(不是100%完成):
labels = []
for i in range(0, len(test)):
name = test.iloc[i]['name']
s = name.split('.')
label_name = s[0]
label = label_names.index(label_name)
id = int(s[1])
labels.append(label)
test['label'] = labels
The last statement generates a "A value is trying to be set on a copy of a slice from a DataFrame." 最后一个语句生成“正在尝试在DataFrame的切片副本上设置值”。 warning.
警告。
Is there a better way to create the new column 'label' in the dataframe? 有没有更好的方法在数据框中创建新列'标签'?
def my_function(input):
name = input
s = name.split('.')
label_name = s[0]
label = label_names.index(label_name)
id = int(s[1])
return label
test['label'] = test['name'].apply(lambda x: my_function(x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.