是否有内置函数可以使用字典作为数据框中的映射器和索引级别来创建
pandas.Series
列?
这个想法是根据索引级别和字典中的值创建一个新列。例如:
假设有以下数据框,其中
id
、name
和 code
以及索引中的不同级别
df
col1 col2
id name code
0 a x 7 10
y 8 11
z 9 12
1 b x 13 16
y 14 17
z 15 18
以及以下词典
d = {'a': {'y', 'z'}, 'b': {'x'}}
新列的输出应如下所示:
col1 col2 new
id name code
0 a x 7 10 0
y 8 11 1
z 9 12 1
1 b x 13 16 1
y 14 17 0
z 15 18 0
作为映射的结果,其中
new
= 1
如果 code
索引值位于具有键 name
的值的字典列表中,否则为 0
。
我试图手动进行此映射,但我不确定如何迭代索引级别。
这是我迄今为止的尝试:
df['y'] = [1 if i in d[k] else 0 for k, v in d.items() for i
in df.index.get_level_values('code')]
但是我收到以下错误,这让我觉得我没有正确迭代索引级别或没有按照预期与字典结合迭代索引级别。
ValueError: Length of values does not match length of index
有什么建议吗?
将其用于您需要的新列:
df['new'] = [1 if j in d[i] else 0 for (i, j) in zip(df.index.get_level_values('name'), df.index.get_level_values('code'))]
@WebDev 的上述答案的一种超级非Pythonic且低效的方式
k = list(zip(df.index.get_level_values('Brand'),
df.index.get_level_values('Metric')))
tmp_list = [0]*df.shape[0]
for keys in d:
for vals in d[keys]:
for i,pairs in enumerate(k):
if pairs[0] == keys and pairs[1] == vals:
tmp_list[i] = 1
df['new'] = tmp_list
pd.DataFrame.from_dict(d, orient='index').stack().reset_index().sql.set_alias("tb2").join(df1.sql.set_alias("tb1"),condition='tb1.name = tb2.level_0 and tb1.code = tb2."0"',how="right").select('tb1.*,case when tb2.level_0 is null then 0 else 1 end "new"').df().set_index(["id","name","code"])
index col1 col2 new
id name code
0 a y 1 8 11 1
z 2 9 12 1
1 b x 3 13 16 1
0 a x 0 7 10 0
1 b y 4 14 17 0
z 5 15 18 0