简体   繁体   中英

reducing a column of CSV lists to a single list

I'm using Python3 to read a column from an Excel spreadsheet:

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,...).

df.col2.ravel() seems to reduce col2 to a list of strings.

df.col2.flatten() yields

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:

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:

  1. 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'], ...] 
  2. Get list with unique items

     unique = list({elem for arr in arrs for elem in arr}) # ['spam', 'sausage', 'egg', 'bacon'] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM