Column and column values ​​in pandas dataframe

I have a dataframe that looks like this, but with 26 rows and 110 columns:

index/io   1   2   3   4
  0        42  53  23  4
  1        53  24  6   12
  2        63  12  65  34
  3        13  64  23  43

Required Conclusion:

index  io  value
0      1   42
0      2   53
0      3   23
0      4   4
1      1   53
1      2   24
1      3   6
1      4   12
2      1   63
2      2   12
... 

I tried with dict and lists, converting the dataframe to dict, and then create a new list with indexes and update in the new dict with io.

indx = []

for key, value in mydict.iteritems():
    for k, v in value.iteritems():
        indx.append(key)
indxio = {}
for element in indx:
    for key, value in mydict.iteritems():
        for k, v in value.iteritems():
            indxio.update({element:k})

I know this is too far away, but this is the only thing I could think of. The process was too long, so I stopped.

+4
source share
2 answers

You can use set_index, stackand reset_index().

df.set_index("index/io").stack().reset_index(name="value")\
  .rename(columns={'index/io':'index','level_1':'io'})

Conclusion:

    index io  value
0       0  1     42
1       0  2     53
2       0  3     23
3       0  4      4
4       1  1     53
5       1  2     24
6       1  3      6
7       1  4     12
8       2  1     63
9       2  2     12
10      2  3     65
11      2  4     34
12      3  1     13
13      3  2     64
14      3  3     23
15      3  4     43
+4
source

You need set_index+ stack+ rename_axis+reset_index

df = df.set_index('index/io').stack().rename_axis(('index','io')).reset_index(name='value')
print (df)
    index io  value
0       0  1     42
1       0  2     53
2       0  3     23
3       0  4      4
4       1  1     53
5       1  2     24
6       1  3      6
7       1  4     12
8       2  1     63
9       2  2     12
10      2  3     65
11      2  4     34
12      3  1     13
13      3  2     64
14      3  3     23
15      3  4     43

melt, rename, , sort_values :

d = {'index/io':'index'}
df = df.melt('index/io', var_name='io', value_name='value') \
       .rename(columns=d).sort_values(['index','io']).reset_index(drop=True)
print (df)
    index io  value
0       0  1     42
1       0  2     53
2       0  3     23
3       0  4      4
4       1  1     53
5       1  2     24
6       1  3      6
7       1  4     12
8       2  1     63
9       2  2     12
10      2  3     65
11      2  4     34
12      3  1     13
13      3  2     64
14      3  3     23
15      3  4     43

numpy:

df = df.set_index('index/io')
a = np.repeat(df.index,  len(df.columns))
b = np.tile(df.columns, len(df.index))
c = df.values.ravel()
cols = ['index','io','value']
df = pd.DataFrame(np.column_stack([a,b,c]), columns = cols)
print (df)
   index io value
0      0  1    42
1      0  2    53
2      0  3    23
3      0  4     4
4      1  1    53
5      1  2    24
6      1  3     6
7      1  4    12
8      2  1    63
9      2  2    12
10     2  3    65
11     2  4    34
12     3  1    13
13     3  2    64
14     3  3    23
15     3  4    43
+4

Source: https://habr.com/ru/post/1681116/


All Articles