从列中的列表中删除方括号

发布于 2025-01-24 10:28:33 字数 675 浏览 0 评论 0原文

我的列有数据框中的列表。我只想删除方括号:

| Fruits                               |
| -------------------------------------|
| [apple, kiwi, pineapple, mango, lime]| 
| [strawberry, cherry, kiwi, orange]   | 
| [...]                                |

所需的输出:

| Fruits                             |
| -----------------------------------|
| apple, kiwi, pineapple, mango, lime| 
| strawberry, cherry, kiwi, orange   | 
| ...                                |

我已经尝试了这

for fruit in df['Fruits']:
    df['Fruits'] = (','.join(word))
    print(df['Fruits'])

不是我想要的输出,以及如何将新列表附加到新列(当然无需方括号)上,任何人都可以提供帮助?

I have column with lists in a dataframe. I want to just remove the square brackets:

| Fruits                               |
| -------------------------------------|
| [apple, kiwi, pineapple, mango, lime]| 
| [strawberry, cherry, kiwi, orange]   | 
| [...]                                |

Desired Output:

| Fruits                             |
| -----------------------------------|
| apple, kiwi, pineapple, mango, lime| 
| strawberry, cherry, kiwi, orange   | 
| ...                                |

I have tried this

for fruit in df['Fruits']:
    df['Fruits'] = (','.join(word))
    print(df['Fruits'])

The output is not what I want, and how can I append the new list to a new column (of course without the need of square brackets) anyone can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2025-01-31 10:28:33

iiuc,尝试以此为字符串存储或列表storge:

# String object
df_string = pd.DataFrame({'Fruits':['[apple, kiwi, pineapple, mango]',
                                     '[strawberry, cherry, kiwi, orange]']})

df_string['Fruits'].str.replace('\[|\]','', regex=True)

输出:

0       apple, kiwi, pineapple, mango
1    strawberry, cherry, kiwi, orange
Name: Fruits, dtype: object

或,

#List object
df_list = pd.DataFrame({'Fruits':[['apple', 'kiwi', 'pineapple', 'mango'],
                                     ['strawberry', 'cherry', 'kiwi', 'orange']]})

df_list['Fruits'].agg(', '.join)

输出:

0       apple, kiwi, pineapple, mango
1    strawberry, cherry, kiwi, orange
Name: Fruits, dtype: object

注意:df_list和df_string字符串表示形式看起来相同。

print(df_list)
                               Fruits
0     [apple, kiwi, pineapple, mango]
1  [strawberry, cherry, kiwi, orange]

print(df_string)
                              Fruits
0     [apple, kiwi, pineapple, mango]
1  [strawberry, cherry, kiwi, orange]

IIUC, try this for string storage or list storge:

# String object
df_string = pd.DataFrame({'Fruits':['[apple, kiwi, pineapple, mango]',
                                     '[strawberry, cherry, kiwi, orange]']})

df_string['Fruits'].str.replace('\[|\]','', regex=True)

Output:

0       apple, kiwi, pineapple, mango
1    strawberry, cherry, kiwi, orange
Name: Fruits, dtype: object

Or,

#List object
df_list = pd.DataFrame({'Fruits':[['apple', 'kiwi', 'pineapple', 'mango'],
                                     ['strawberry', 'cherry', 'kiwi', 'orange']]})

df_list['Fruits'].agg(', '.join)

Output:

0       apple, kiwi, pineapple, mango
1    strawberry, cherry, kiwi, orange
Name: Fruits, dtype: object

Note: df_list and df_string string representation looks identical.

print(df_list)
                               Fruits
0     [apple, kiwi, pineapple, mango]
1  [strawberry, cherry, kiwi, orange]

print(df_string)
                              Fruits
0     [apple, kiwi, pineapple, mango]
1  [strawberry, cherry, kiwi, orange]
朕就是辣么酷 2025-01-31 10:28:33

您可以尝试使用[]字符字符字符,并用repleent()函数替换 carture function:

for fruit in df['Fruits']:
    df['Fruits'] = df['Fruits'].replace("[", "").replace("]", "")
    print(df['Fruits'])

You can try to replace the [] characters by empty characters with the replace() function:

for fruit in df['Fruits']:
    df['Fruits'] = df['Fruits'].replace("[", "").replace("]", "")
    print(df['Fruits'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文