Change column values to column names without duplicate and append a list od values to each cell

Kuki :

I have a 400 rows long dataframe that looks something like this:

Name  Colour   Rating
V001  Blue      5
V001  Green     4
V001  Blue      5
V002  Red       2
V002  Yellow    6
V002  Yellow    5
V003  Blue      7
V003  Blue      1
V003  Green     2

What i want, is to convert values in Colour column to column names without duplicate, and append list of all values corresponding to that color. I also want to have only one row for one Name. New dataframe would look like this:

Name   Blue  Green  Red  Yellow
V001  [5,5]  [4]    []    []
V002  []     []     [2]   [6,5]
V003  [7,1]  [2]    []    []
jezrael :

Use DataFrame.pivot_table with replace missing values to empty lists:

df = df.pivot_table(index='Name', columns='Colour', values='Rating', aggfunc=list)
df = df.applymap(lambda x: [] if isinstance(x, float) else x)
print (df)
Colour    Blue Green  Red  Yellow
Name                             
V001    [5, 5]   [4]   []      []
V002        []    []  [2]  [6, 5]
V003    [7, 1]   [2]   []      []

For ranges is possible use:

for c in df.columns:
    for i in df.index:
        v = df.loc[i, c]
        #print (v)
        if len(v) > 1:
            print (range(v[1], v[0]))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169682&siteId=1