如何在熊猫中将1-1映射更改为1对多

时间:2018-07-16 16:33:52

标签: python pandas dataframe

我有以下数据框。我想将相似的外观名称映射到一栏中。但是这些分布在两列中。

df:

state   city            alt_cities
FL      FORT PIERCE     FT. PIERCE
FL      FORTPIERCE      FT. PIERCE
FL      FT.PIERCE       FT. PIERCE
FL      FORTPIERCE      FORT PIERCE
FL      FT PIERCE       FORT PIERCE
FL      FT. PIERCE      FORT PIERCE
FL      FT.PIERCE       FORT PIERCE
FL      FTPIERCE        FORT PIERCE
AL      ATTALIA         ATTALLA
AL      ATTALLA         ATTALLA

如何映射这些名称以获得以下结果?

out_df:

state   city            alt_cities   city_var
FL      FORT PIERCE     FT. PIERCE   FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FORTPIERCE      FT. PIERCE   FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FT.PIERCE       FT. PIERCE   FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FORTPIERCE      FORT PIERCE  FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FT PIERCE       FORT PIERCE  FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FT. PIERCE      FORT PIERCE  FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FT.PIERCE       FORT PIERCE  FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
FL      FTPIERCE        FORT PIERCE  FORT PIERCE@FORTPIERCE@FT.PIERCE@FT PIERCE@FT. PIERCE@FTPIERCE
AL      ATTALIA         ATTALLA      ATTALIA@ATTALLA 
AL      ATTALLA         ATTALLA      ATTALIA@ATTALLA

是否有实现此目的的简短方法?另一个选择是将它们转换为字典,然后进行映射。

提前谢谢!

0 个答案:

没有答案