[英]reducing a column of CSV lists to a single list
I'm using Python3 to read a column from an Excel spreadsheet: 我正在使用Python3从Excel电子表格中读取一列:
import pandas as pd
from pandas import ExcelFile
df = pd.read_excel('MWE.xlsx', sheet_name='Sheet1')
print(df)
col1 col2
0 starts normal egg, bacon
1 still none the wiser egg, sausage, bacon
2 maybe odd tastes egg, spam
3 or maybe post-war egg, bacon, spam
4 maybe for the hungry egg, bacon, sausage, spam
5 bingo spam, bacon, sausage, spam
I want to reduce col2
to a single list of the words in col2 (eg egg, bacon,...). 我想将
col2
简化为col2
中单词的单个列表(例如egg,腊肉,...)。
df.col2.ravel()
seems to reduce col2
to a list of strings. df.col2.ravel()
似乎将col2
简化为字符串列表。
df.col2.flatten()
yields df.col2.flatten()
产生
AttributeError: 'Series' object has no attribute 'flatten'
If what you want is to have a Series of list as the col2, this will do the trick: 如果您想要将一系列列表作为col2,则可以解决这个问题:
df = pd.DataFrame({'col1': ['starts normal','still none the wiser'], 'col2': ['egg, bacon','egg, sausage, bacon']})
df['col2'] = df['col2'].map(lambda x: [i.strip() for i in x.split(',')])
print(df)
Result: 结果:
col1 col2
0 starts normal [egg, bacon]
1 still none the wiser [egg, sausage, bacon]
Try something simple like: 尝试一些简单的事情,例如:
df = pd.DataFrame({'col2': [list('abc'), list('de'), list('fghi')]})
flat_col2 = [element for row in df.col2 for element in row]
# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
Maybe this is what you need: 也许这是您需要的:
Turn series of comma separated strings into a list of lists 将一系列用逗号分隔的字符串转换为列表列表
arrs = df.col2.map(lambda x: [i.strip() for i in x.split(',')]).tolist() # [['egg', 'bacon'], ['egg', 'sausage', 'bacon'], ...]
Get list with unique items 获取包含唯一项的列表
unique = list({elem for arr in arrs for elem in arr}) # ['spam', 'sausage', 'egg', 'bacon']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.