【问题标题】:Need to convert the Pandas DataFrame to list of lists?需要将 Pandas DataFrame 转换为列表列表吗?
【发布时间】:2017-08-15 21:40:41
【问题描述】:

我已使用以下代码获取列表。

 df['Column'].values.tolist()

Pandas DataFrame 像这样返回数据

['tropical fruit,yogurt,coffee', 'pip fruit','yogurt,cream cheese ,meat spreads', 'whole milk','butter,yogurt,rice,abrasive cleaner']

但我需要像这样获取数据

[['tropical fruit','yogurt','coffee'],['pip fruit'],['yogurt','cream cheese' ,'meat spreads'],['whole milk'],['butter','yogurt','rice','abrasive cleaner']]

【问题讨论】:

  • 请提供一些数据。
  • 你想分开热带和水果而不是奶油奶酪?
  • 没有。数据从一列返回,该列有多个字符串,用逗号“,”分割
  • @WannaBeCoder 抱歉,这是错误的。我改变了问题。热带和水果是同一个字符串。无需拆分。

标签: python list pandas dataframe


【解决方案1】:
[ s.split(',') for s in df['Column'].values.tolist()]

或者可能

df['Column'].str.split(',').values.tolist()

【讨论】:

  • 这个答案返回我想要的。
猜你喜欢
  • 1970-01-01
  • 2021-03-01
  • 2013-11-04
  • 2019-11-28
  • 2013-02-13
  • 2021-08-30
  • 2014-01-05
  • 2021-11-28
相关资源
最近更新 更多