3 回答

TA贡献1856条经验 获得超17个赞
这是你想要的吗?
df = df.set_index('id')
dictionary = {1:[5,8,6,3], 2:[1,2], 5:[8,6,2]}
df['new_column'] = pd.Series(dictionary)
注意:字典的键需要与数据框的索引具有相同的类型(int)。
>>> print(df)
gender new_column
id
1 0 [5, 8, 6, 3]
2 0 [1, 2]
3 1 NaN
4 1 NaN
5 1 [8, 6, 2]
更新:
如果列包含重复项,则更好的解决方案'id'(请参阅下面的评论):
df['new_column'] = df['id'].map(dictionary)

TA贡献1789条经验 获得超10个赞
import pandas as pd
df = pd.DataFrame({'id':[1,2,3,4,5], 'gender':[0,0,1,1,1]})
dictionary = {'1':[5,8,6,3], '2':[1,2], '5':[8,6,2]}
然后只需创建一个包含您想要的值的列表并将它们添加到您的数据框中
newValues = [ dictionary.get(str(val),[]) for val in df['id'].values]
df['new_column'] = newValues
>>> print(df)
gender new_column
id
1 0 [5, 8, 6, 3]
2 0 [1, 2]
3 1 []
4 1 []
5 1 [8, 6, 2]

TA贡献1946条经验 获得超4个赞
[]您可以使用默认具有值的特殊字典来构造您的列。
from collections import defaultdict
default_dictionary = defaultdict(list)
id = [1,2,3,4,5]
dictionary = {'1':[5,8,6,3], '2':[1,2], '5':[8,6,2]}
for n in dictionary:
default_dictionary[n] = dictionary[n]
new_column = [default_dictionary[str(n)] for n in id]
new_column 是[[5, 8, 6, 3], [1, 2], [], [], [8, 6, 2]]现在,你可以把它传递给你的最后一个论点pd.DataFrame(...)
添加回答
举报