Home > Enterprise >  Pandas Convert tuples into Columns
Pandas Convert tuples into Columns

Time:04-11

I'm trying to get the data from the previous 14 days based on one row and add each value to it's own column. I have made code that will retrieve this data and return it as a list of tuples. I'm having trouble converting this list of tuples into their own columns.

I have included some dummy data and the function in question. 'row' is what the output of RetrievePreviousData will output, and that will be the output for every row in my main dataframe.

row = [(0, '9.3', '0'), (1, '10.7', '0.4'), (2, '10.9', '7.4'), (3, '8.9', '4.8'), (4, '12.2', '7.4'), (5, '11.5', '2.6'), (6, '9.7', '7.4'), (7, '7.6', '7.4'), (8, '8', '5.4'), (9, '10', '0'), (10, '13.1', '5.8'), (11, '7.9', '9.8'), (12, '8.5', '9.8'), (13, '6.7', '13')]

def RetrievePreviousData(name, date):
    dataList = []
    max_days = 14
    for i in range(max_days):
        
        data_test0 = RetrieveData(name, date-datetime.timedelta(days=i), True) 
        data_test1 = RetrieveData(name, date-datetime.timedelta(days=i), False)
        dataList.append((i, data_test0, data_test1))
    
    return dataList

Applying pd.Series puts every element of the array into a column, but I want the elements of the tuple in their own column with the first element used to name the columns of the following elements. The expected output would be:

0 Test0 | 0 Test1| 1 Test0 | 1 Test1 | etc
9.3     | 0      | 10.7    | 0.4     | etc

CodePudding user response:

Hi you can straight away convert a tuple to pandas data frame.

import pandas as pd

row = [(0, '9.3', '0'), (1, '10.7', '0.4'), (2, '10.9', '7.4'), (3, '8.9', '4.8'), (4, '12.2', '7.4'), (5, '11.5', '2.6'), (6, '9.7', '7.4'), (7, '7.6', '7.4'), (8, '8', '5.4'), (9, '10', '0'), (10, '13.1', '5.8'), (11, '7.9', '9.8'), (12, '8.5', '9.8'), (13, '6.7', '13')]

df = pd.DataFrame(row, columns=['Col 1', 'Col 2', 'Col 3'])

CodePudding user response:

I managed to get the desired output with this:

names = ['Test0', 'Test1']
def GetListTupleValue(listIndex, tupleIndex, tupleToGet):
    print(tupleToGet[listIndex][tupleIndex])
    return tupleToGet[listIndex][tupleIndex]

for i in range(max_days):
    for j in range(len(names)):
        name = str(i)   ' Prev '   names[j]
        waid[name] = [GetListTupleValue(i,j 1, x) for x in df[col]]

If anyone can think of a more pythonic way of doing this, please be my guest but this works for me

CodePudding user response:

If you just have one row you could try

df = pd.DataFrame({f"{c} test{i}": [v] for c, *vals in row for i, v in enumerate(vals)})

Result for your sample row:

  0 test0 0 test1 1 test0 1 test1  ... 12 test0 12 test1 13 test0 13 test1
0     9.3       0    10.7     0.4  ...      8.5      9.8      6.7       13

[1 rows x 28 columns]

If you have several rows, then you could try

df = pd.DataFrame(
    {f"{c} test{i}": v for c, *vals in row for i, v in enumerate(vals)}
    for row in rows
)

Result for rows = [row, row]:

  0 test0 0 test1 1 test0 1 test1  ... 12 test0 12 test1 13 test0 13 test1
0     9.3       0    10.7     0.4  ...      8.5      9.8      6.7       13
1     9.3       0    10.7     0.4  ...      8.5      9.8      6.7       13

[2 rows x 28 columns]
  • Related