0

I want to fetch the data of the column "Examples" with respect to column " Category"

enter image description here

Output:
Fruits [Apple,Mango,Orange,Mosambi]

Veggies [Tomato,Onion,Brinjal]

Readymade [Maggi,Ab,Mixes,Foh]
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • 1
    Please read [why-not-upload-images-of-code](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) and [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). After it, please your question using `edit` option. – Brown Bear Jun 09 '21 at 11:29

2 Answers2

1

If I understood correct, you want to unpack each list that contains a few lists in the Example column.

One way is to use numpy's ravel function. Assuming your dataframe is df:

import numpy as np
df["Examples"] = df["Examples"].apply(lambda x: np.concatenate(x).ravel())

    Category    Examples
0   Fruits  [Apple, Mango, Orange, Mosambi]
1   Veggies [Tomato, Onion, Brinjal]
2   Readymade   [Maggi, Ab, Mixes, Foh]

Edit:

As per the comment, some elements in the Example column are not list of lists (my above assumption is wrong). That can be handled by adding a check like below:

df["Examples"].apply(lambda x: np.concatenate(x).ravel() if all(isinstance(i, list) for i in x) and len(x)!=0 else x)

Note: There can be many combinations of possibilities here. The above check will assume if all the entries in a list are not lists, then they are just strings and also handles the empty list. But, there can be cases where an element is not a list but of some other data type, and then this check will be wrong.

Demo:

(As per the next comment, if a collection of strings are preferred instead of a list, applying df["Examples"].apply(lambda x: ','.join(x)) would do that.

df = pd.DataFrame([
    ["Fruits", [["Apple","Mango"],["Orange","Mosambi"]]],
    ["Veggies", [["Tomato","Onion"],["Brinjal"]]],
    ["Readymade", [["Maggi","Ab"],["Mixes","Foh"]]],
    ["Test", ["a", "b"]],
    ["Testt", []]
    ],
    columns=["Category", "Examples"]
)
df["Examples"].apply(lambda x: np.concatenate(x).ravel() if all(isinstance(i, list) for i in x) and len(x)!=0 else x)
df["Examples"] = df["Examples"].apply(lambda x: ','.join(x))
df

Category    Examples
0   Fruits  Apple,Mango,Orange,Mosambi
1   Veggies Tomato,Onion,Brinjal
2   Readymade   Maggi,Ab,Mixes,Foh
3   Test    a,b
4   Testt   
akilat90
  • 5,436
  • 7
  • 28
  • 42
  • ValueError: zero-dimensional arrays cannot be concatenated – Gayathri Kumar Jun 09 '21 at 12:53
  • How to make the values in "Example" column as string seperated by comma . instead of list – Gayathri Kumar Jun 09 '21 at 13:01
  • @GayathriKumar You will get `ValueError: zero-dimensional arrays cannot be concatenated` if some entries in `df['Example']` are not list of lists but just a list - I just assumed that it's not the case from the information you've given. Probably, you have a list (not a list of lists) in that column. – akilat90 Jun 09 '21 at 13:05
  • @GayathriKumar Check the updated answer. Next time, please include all your requirements in the question itself :) – akilat90 Jun 09 '21 at 13:23
0

You can use .flatten()

df['examples_flat'] = df['examples'].apply(lambda x: np.asarray(x).flatten())

[UPDATE]

To get values as strings separated by commas, instead of list, use ", ".join()

df['examples_flat'] = df['examples'].apply(lambda x: ", ".join(np.asarray(x).flatten()))


    category    examples            examples_flat
0   fruits      []                  
1   veggies     [[e, f], [g, h]]    e, f, g, h
2   readymade   [[i, j], [k, l]]    i, j, k, l
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26
  • I think `np.asarray` will not work for the OP's example where lists contain lists that are of different lengths. See the example dataframe in my answer. – akilat90 Jun 09 '21 at 13:28