我有一个看起来像这样的列表:
some_list = [["breakfast", ["apple", 'pear', 'cereal']], ["two", ["ham", 'burger', 'chicken wings']]]
我把它做成了熊猫数据框:
df = pd.DataFrame(some_list,columns=['one', 'two'])
打印它会显示以下内容:
one two
0 breakfast [apple, pear, rice]
1 dinner [ham, burger, chicken wings]
我的问题
是否可以从第二列的项目中删除列表格式(即[[和']')?
答案 0 :(得分:3)
您可以将列转换为字符串,并用逗号将其连接起来。
df.two = df.two.str.join(',')
输出将是:
one two
0 breakfast apple,pear,cereal
1 dinner ham,burger,chicken wings
在上面的示例中,如果列表包含类型不是str的对象,则结果将产生NaN
示例:
import pandas as pd
some_list = [["breakfast", ["apple", 'pear', 'cereal']],
["dinner", ["ham", 'burger', 'chicken wings']],
["lunch", [20, 'pasta', 'french fries']]]
df = pd.DataFrame(some_list,columns=['one', 'two'])
df.two = df.two.str.join(',')
print (df)
输出将是:
one two
0 breakfast apple,pear,cereal
1 dinner ham,burger,chicken wings
2 lunch NaN
或者,您可以使用map和join对其进行扩展。这不会导致第三行的NaN(午餐)
import pandas as pd
some_list = [["breakfast", ["apple", 'pear', 'cereal']],
["dinner", ["ham", 'burger', 'chicken wings']],
["lunch", [20, 'pasta', 'french fries']]]
df = pd.DataFrame(some_list,columns=['one', 'two'])
#df.two = df.two.str.join(',')
df['two'] = [','.join(map(str, x)) for x in df['two']]
print (df)
如果您更熟悉apply和lambda,那么:
df['two'] = df['two'].apply(lambda x: ','.join(map(str, x)))
输出将是:
one two
0 breakfast apple,pear,cereal
1 dinner ham,burger,chicken wings
2 lunch 20,pasta,french fries
答案 1 :(得分:2)
您可以使用以下方法将列表值连接到字符串:
df['two'] = df['two'].agg(','.join)