Home > Enterprise >  From tuple's list to dataframes
From tuple's list to dataframes

Time:01-05

So I have a question about creating dataframes of these types of lists. I'm working with a dataset that resembles the mentioned example (although much larger).

data = [("A", (1, 2, 3)), ("B", (5, 7, 9)), ("C", (2, 4, 6))]

I've tried to make it look like a dataframe that looks like this:

A  B  C
1  5  2
2  7  4
3  9  6

I've explored the usage of

pd.DataFrame()

But it didn't seem to work. Any suggestions?

CodePudding user response:

This may not be the most efficient way but you can convert your data into a dict and then to a DataFrame.

import pandas as pd

data = [("A", (1, 2, 3)), ("B", (5, 7, 9)), ("C", (2, 4, 6))]

# convert to dictionary then to dataframe
df = pd.DataFrame(dict(data))

CodePudding user response:

You can cast your data to pd.DataFrame, then use set_index method to set the index to the letters in each tuple, then transpose the dataframe and use the explode method to explode the inner tuples to separate rows. Then use reset_index and rename_axis methods to get the desired outcome.

data = [("A", (1, 2, 3)), ("B", (5, 7, 9)), ("C", (2, 4, 6))]
df = pd.DataFrame(data).set_index(0).T.explode(['A','B','C']).reset_index(drop=True).rename_axis(columns={0:None})

Another option is to use apply(pd.Series) and transpose:

df = pd.DataFrame(data).set_index(0)[1].apply(pd.Series).T

Yet another way. You can separate names from data using zip and cast to pd.DataFrame:

idx, data = zip(*data)
df = pd.DataFrame(data, index=idx).T

Output:

   A  B  C
0  1  5  2
1  2  7  4
2  3  9  6
  •  Tags:  
  • Related