1

I have a Pandas DataFrame which looks like this:

Time Image_names
0    [a,b,c,d]
0    [a,c,d,e]
0    [c,d,e,f]
1    [e,f,g,h]
1    [f,g,h,i]

What I wish to obtain: All unique image names for a given Time

Time Image_names
0    [a,b,c,d,e]
1    [e,f,g,h,i]

I'm not sure if I have to use groupby or joins.

T

1
  • From @jpp, all your need is: df.groupby('Time')['Image_names'].apply(lambda x: set(chain.from_iterable(x))) Commented Feb 26, 2018 at 17:44

3 Answers 3

1

You can using set

s=df.groupby('Time',as_index=False).Image_names.sum()
s.Image_names=list(map(set,s.Image_names))
s
Out[2034]: 
   Time         Image_names
0     0  {b, c, d, a, f, e}
1     1     {g, h, f, i, e}
Sign up to request clarification or add additional context in comments.

2 Comments

this works great! But once I write this data to a CSV using df.to_csv("resultsDf.csv"), the Images_names appears as set([b,c,d,a,f,e])
@JagannathSaragadam adding s.Image_names=s.Image_names.apply(list) then to_csv :-)
1

One way is to use itertools.chain:

from itertools import chain
import pandas as pd


df = pd.DataFrame({'Time': [0, 0, 0, 1, 1],
                   'Image_names': [['a', 'b', 'c', 'd'],
                                   ['a', 'c', 'd', 'e'],
                                   ['c', 'd', 'e', 'f'],
                                   ['e', 'f', 'g', 'h'],
                                   ['f', 'g', 'h', 'i']]})

df = df.groupby('Time')['Image_names'].apply(chain.from_iterable).map(set).reset_index()

#    Time         Image_names
# 0     0  {c, a, f, d, e, b}
# 1     1     {g, h, f, e, i}

Explanation

  • Applying chain.from_iterable joins the lists from each group into one large list for each group.
  • Mapping set then creates a set for each group.
  • reset_index ensures the result is a dataframe with column headers as required.

Comments

0

You can use the following:

import pandas as pd
import numpy as np

a=pd.DataFrame([[0,['a','b','c','d']],[0,['a','c','d','e']],
                [0,['c','d','e','f']],[1,['e','f','g','h']],
                [1,['f','g','h','i']]],
                columns=['Time','Image_names'])
a.groupby('Time')['Image_names'].sum().apply(np.unique)

#Out[242]: 
#Time
#0    [a, b, c, d, e, f]
#1       [e, f, g, h, i]
#Name: Image_names, dtype: object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.