0

I've this issue and i've been doing quite a few researches and doing some testing without success.

I've a Dataframe which one of the variables content looks like this:

var1 = ['QALIF',
 ['PRD', 'PRD', 'PRD', 'PRD', 'PRD'],
 ['DV', 'DV', 'DV', 'DV', 'DV', 'DV', 'DV']]

I don't know how represent this in a dataframe, when I convert into pd.DataFrame(var1) the Data Frame splits into several columns which is not the case. The values are type text for Single values or liste of values with different lengths.

I need to get unique values for each list inside my Dataframe, in other words, iterate over my variable "var1" and convert the "List" into Single value

I've been trying to get some inspiration thought this link below, some test did work fine but when I try to apply to entire dataset it doesn't return good results.

Get unique values from a list in python

I've build a (several) functions :

for lst in df[var1]:
     df[var1].apply(lambda x: list(dict.fromkeys(lst)))

But I get an error

List object is not callable

And I've tried so many other options but none of them seem to work.

Any help is welcome

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Carlos Carvalho
  • 131
  • 1
  • 8

2 Answers2

0
var1 = ['QALIF',
 ['PRD', 'PRD', 'PRD', 'PRD', 'PRD'],
 ['DV', 'DV', 'DV', 'DV', 'DV', 'DV', 'DV']]
import pandas as pd
var2=[]
def frame(var1):
    if type(var1)==list and len(var1)>1:
        return var1[0]
    return var1



df=pd.DataFrame(var1)
      
df[0]=df[0].apply(frame)

print(df)
Chinu
  • 11
  • 1
  • @Chimu Var1 is an column from a Dataframe, and the values from this column are text, but some others are lists inside brackets [ ] , so df['var1'] it's a series – Carlos Carvalho Jun 08 '21 at 14:26
0

If what you want is a Seriescontaining all the unique items inside the lists you can use the explode method (Documentation):

var1 = ['QALIF', ['PRD', 'PRD', 'PRD', 'PRD', 'PRD'], ['DV', 'DV', 'DV', 'DV', 'DV', 'DV', 'DV']]

df = pd.DataFrame(var1).explode(0).drop_duplicates()

print(df)

output:

       0
0  QALIF
1    PRD
2     DV
99_m4n
  • 1,239
  • 3
  • 17