Home > front end >  List of whole pandas values referenced by column and index
List of whole pandas values referenced by column and index

Time:05-11

I have a pandas dataframe and I want to get a list or array of values and their relative positions in the dataframe. So I have:

index A B
0 2 5
1 1 4
2 6 3

I want to get

[1,1,A][2,0,A][3,2,B][4,1,B][5,0,B][6,2,A]

Any help?

Thank you very much

CodePudding user response:

You can do reset_index then melt

out = df.reset_index().melt('index')[['value','index','variable']].to_numpy().tolist()
Out[533]: [[2, 0, 'A'], [1, 1, 'A'], [6, 2, 'A'], [5, 0, 'B'], [4, 1, 'B'], [3, 2, 'B']]

CodePudding user response:

data = {'A': [2, 1, 6], 'B': [5, 4, 3]}
df = pd.DataFrame(data)

list_ = []

for i in range(len(df)):
    for j in range(len(df.columns)):
        value = df.loc[i][j]
        index_ = i
        column_name = df.columns.values.tolist()[j]

        list_.append([value, index_, column_name])
    
print(list_)

Result:

[[2, 0, 'A'], [5, 0, 'B'], [1, 1, 'A'], [4, 1, 'B'], [6, 2, 'A'], [3, 2, 'B']]

CodePudding user response:

Another option:

df.set_index('index').stack().sort_values().reset_index().iloc[:, [2,0,1]].values.tolist()

NB. if "index" is not a column but the index, remove the .set_index('index')

df.stack().sort_values().reset_index().iloc[:, [2,0,1]].values.tolist()

output:

[[1, 1, 'A'], [2, 0, 'A'], [3, 2, 'B'], [4, 1, 'B'], [5, 0, 'B'], [6, 2, 'A']]
  • Related