python 序列后的字符串连接

python string concatenation following a sequence

创建以下字符串连接的最 pythonic 方法是什么:

我们有一个初始数据框,其中一些列是:

  1. 起源
  2. dest_1_country
  3. dest_1_city
  4. dest_2_country
  5. dest_2_city
  6. dest_3_country
  7. dest_3_city
  8. dest_4_country
  9. dest_4_city

我们想创建一个额外的列,它是数据框中每一行的完整路线,可以由

生成

df['full_route'] = df['origin].fillna("") + df['dest_1_country].fillna("") + df['dest_1_city .fillna("") + df['dest_2_country].fillna("") + df['dest_2_city].fillna("") + df['dest_3_country]。 fillna("") + df['dest_3_city].fillna("") + df['dest_4_country].fillna("") + df['dest_4_city].fillna( "")

显然这不是获得所需结果的最pythonic 方法,因为它非常麻烦。如果我在 df 中有 100 个城市怎么办?

在 python 中实现此目标的最佳方法是什么?

注意:在数据框中,还有其他列与路由无关,不应在串联中考虑。

非常感谢!!

如果你有这个数据框:

  origin dest_1_country dest_1_city dest_2_country dest_2_city
0      a              b           c              d           e
1      f              g           h              i           j

那么你可以这样做:

df["full_route"] = df.sum(axis=1)  # df.fillna("").sum(axis=1) if you have NaNs
print(df)

连接所有列:

  origin dest_1_country dest_1_city dest_2_country dest_2_city full_route
0      a              b           c              d           e      abcde
1      f              g           h              i           j      fghij

编辑:如果你想连接“origin”和每个“*city”/“*country”列,你可以这样做:

df["full_route"] = df["origin"].fillna("") + df.filter(
    regex=r"country$|city$"
).fillna("").sum(axis=1)
print(df)