【发布时间】:2020-12-23 03:00:01
【问题描述】:
我正在使用需要替换 1 列中的值的数据框。我的本能是去找一个 python 字典但是,这是我的数据看起来像的一个例子(original_col):
original_col desired_col
cat animal
dog animal
bunny animal
cat animal
chair furniture
couch furniture
Bob person
Lisa person
字典看起来像:
my_dict: {'animal': ['cat', 'dog', 'bunny'], 'furniture': ['chair', 'couch'], 'person': ['Bob', 'Lisa']}
我不能使用典型的 my_dict.get(),因为我要检索相应的 KEY 而不是值。字典是最好的数据结构吗?有什么建议吗?
【问题讨论】:
-
你需要 reverse 映射。
标签: python-3.x pandas dataframe dictionary data-structures