Pandas Dataframe :如何从A列中获取值来连接B列中列表的每个值?

gab6jxml  于 2023-03-11  发布在  其他
关注(0)|答案(2)|浏览(129)

比如说,我有一个数据框架:

| ColumnA  | Column B |
|----------|----------|
| prefix_1 | [A, B]   |
| prefix_2 | [C, D]   |

我希望得到一个新的DataFrame,如下所示:

| ColumnA  | Column B | Column C                 |
|----------|----------|--------------------------|
| prefix_1 | [A, B]   | [prefix_1-A, prefix_1-B] |
| prefix_2 | [C, D]   | [prefix_2-C, prefix_2-D] |

我该怎么转账?谢谢。
我尝试了下面,但没有工作:

df['ColumnC'] = df['ColumnB'].str.split(',').apply(lambda x: [df['ColumnA'] + '-' + e.strip() for e in x]).tolist()
mm9b1k5b

mm9b1k5b1#

您使用lambda函数是正确的,但我将这样使用它:

# Create the dataframe from your question
df = pd.DataFrame({'ColumnA': ['prefix_1', 'prefix_2'], 'Column B': [['A', 'B'], ['C', 'D']]})

# Create Column C accordingly
df['Column C'] = df.apply(lambda row: [row['ColumnA'] + '-' + elem for elem in row['Column B']], axis=1)

确保使用axis=1,以便lambda函数按行应用。

gcxthw6b

gcxthw6b2#

使用列表解析。
如果B列中有列表:

df['Column C'] = [[f'{p}-{x}' for x in l] for p, l in
                   zip(df['ColumnA'], df['Column B'])]

如果B列中有字符串:

df['Column C'] = [[f'{p}-{x}' for x in l] for p, l in
                   zip(df['ColumnA'],
                       df['Column B'].str[1:-1].str.split(',\s*')
                       )]

如果你想要一个字符串作为输出:

df['Column C'] = ['['+', '.join([f'{p}-{x}' for x in l])+']'
                  for p, l in
                   zip(df['ColumnA'],
                       df['Column B'].str[1:-1].str.split(',\s*')
                       )]

输出:

ColumnA Column B                  Column C
0  prefix_1   [A, B]  [prefix_1-A, prefix_1-B]
1  prefix_2   [C, D]  [prefix_2-C, prefix_2-D]

可重现输入:

# as string
df = pd.DataFrame({'ColumnA': ['prefix_1', 'prefix_2'],
                   'Column B': ['[A, B]', '[C, D]']})

# as lists
df = pd.DataFrame({'ColumnA': ['prefix_1', 'prefix_2'],
                   'Column B': [['A', 'B'], ['C', 'D']]})

相关问题