Python:根据列与列表连接列

标签 python pandas dataframe

我有一个如下所示的DataFrame:

df
      A    B     C    D    E    key
0  test    Z  10.0    a    a  10111
1  test    A  10.0    a    a  10111
2  test    x   2.0    a    b  11010
3  test    5  12.0    b    b  10100
4  test    x   5.0    c    b  11000
5  test    2  14.0    g    c  10111

我需要得到的是根据 key 列连接所有字符串:

    位置 [0] 处的
  • key 用于 col Akey 位于位置 [1 ] 用于 col B 等等...
  • 中的每个1用于获取,每个0用于跳过列

结果应如下所示:

      A    B     C    D    E    key     key_val
0  test    Z  10.0    a    a  10111  test10.0aa
1  test    A  10.0    a    a  10111  test10.0aa
2  test    x   2.0    a    b  11010      testxa
3  test    5  12.0    b    b  10100    test12.0
4  test    x   5.0    c    b  11000       testx
5  test    2  14.0    g    c  10111  test14.0gc

到目前为止我所做的 - 我创建了 key_list 列:

df['key_list'] = df['key'].apply(lambda x: list(str(x)))

df
      A  B     C  D  E    key         key_list
0  test  Z  10.0  a  a  10111  [1, 0, 1, 1, 1]
1  test  A  10.0  a  a  10111  [1, 0, 1, 1, 1]
2  test  x   2.0  a  b  11010  [1, 1, 0, 1, 0]
3  test  5  12.0  b  b  10100  [1, 0, 1, 0, 0]
4  test  x   5.0  c  b  11000  [1, 1, 0, 0, 0]
5  test  2  14.0  g  c  10111  [1, 0, 1, 1, 1]

下一步我已经尝试过这个(我想乘以 1 或 0 来包含或排除字符串):

df.apply((df['A'].astype(str) * df['key_list'][0]) +
         (df['B'].astype(str) * df['key_list'][1]) +
         (df['C'].astype(str) * df['key_list'][2]) +
         (df['D'].astype(str) * df['key_list'][3]) +
         (df['E'].astype(str) * df['key_list'][4]), axis=1)

但这似乎是错误的想法:ValueError:操作数无法与形状 (6,) (5,) 一起广播。我遵循字符串连接的常见做法,只是需要额外的步骤:

df['A'].astype(str) + df['B'].astype(str) + df['C'].astype(str) + df['D'].astype(str) + df['E'].astype(str)

最佳答案

想法是将key列转换为掩码,然后用DataFrame.where中的空字符串替换不匹配的内容。并将 join 相加:

c = ['A','B','C','D','E']

L = [list(str(x)) for x in df['key']]
m = pd.DataFrame(L, columns=c, index=df.index).fillna(0).astype(int).astype(bool)
print (m)
      A      B      C      D      E
0  True  False   True   True   True
1  True  False   True   True   True
2  True   True  False   True  False
3  True  False   True  False  False
4  True   True  False  False  False
5  True  False   True   True   True

df['key_val'] = df[c].where(m, '').astype(str).sum(axis=1)
print (df)
      A  B     C  D  E    key     key_val
0  test  Z  10.0  a  a  10111  test10.0aa
1  test  A  10.0  a  a  10111  test10.0aa
2  test  x   2.0  a  b  11010      testxa
3  test  5  12.0  b  b  10100    test12.0
4  test  x   5.0  c  b  11000       testx
5  test  2  14.0  g  c  10111  test14.0gc

关于Python:根据列与列表连接列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60165252/

相关文章:

python - 通过索引符号而不是列名称选择数据框中的列

python - 将 1 个常量值添加到 python 列表中的所有 sub_list

python - 根据字符串列过滤分组数据框中的行

python - 如何根据数据框和 numpy 中的协变量对观察结果进行分类?

r - 如何从大型数据框中删除 NA,但保留分隔列的结构?

Python3 : Trying to upgrade pip3 in Ubuntu 16. 04 但它不更新

python - 在 Python 中寻找 print 的替代方案

python - 将 Excel 读入数据框并将文件名保留为列(Pandas)

python - 根据第三列设置列中具有共同值的行的日期

python - 如何根据前一行通过逐行计算改进 DataFrame 上的 for 循环?