Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert Pandas DataFrame to one row without for loops

I got a pandas dataframe like this:

    x   y   z
0   a   d   g
1   b   e   h
2   c   f   i

Now I want to convert it into a dataframe with a single row with each cell and row + column as column names:

    z_2 z_1 z_0 y_2 y_1 y_0 x_2 x_1 x_0
0   i   h   g   f   e   d   c   b   a

I know I can do it like this, but I need to runtime optimize the code, if possible without loops, etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.DataFrame({"x": ["a", "b", "c"],
                    "y": ["d", "e", "f"],
                    "z": ["g", "h", "i"]})
df.to_dict()
wantedRes = pd.DataFrame()
for key, value in df.items():
    for key2, value2 in value.items():
        wantedRes.insert(loc = 0, column = str(key) + "_" + str(key2),value = [value2] ) 

>Solution :

You can use .stack() for this:

s = df.stack()
df_new = pd.DataFrame([s.values],  columns=[f'{j}_{i}' for i, j in s.index])

Output:

  x_0 y_0 z_0 x_1 y_1 z_1 x_2 y_2 z_2
0   a   d   g   b   e   h   c   f   i
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading