How to sort values according to their parameter name and create a table in Python?

Shudras

I have a huge list of strings (similar to strs given below, but much larger). The time stamps are given for each column.
I'd like to efficiently convert it to a table format (numpy array or pandas dataframe or ...) according to the one below.

strs = ['time', 'stamp1', 'a', '1',    'b', '2',    'c', '3', 
        'time', 'stamp2', 'a', '11',   'b', '22',                'd', '4', 
        'time', 'stamp3', 'a', '111',  'b', '222',  'c', '333', 
        'time', 'stamp4', 'a', '1111', 'b', '2222', 'c', '3333', 'd', '444']
time a b c d
stamp1 1 2 3
stamp2 11 22 4
stamp3 111 222 333
stamp4 1111 2222 3333 444
Timus

You could do:

import pandas as pd

records = []
record = {strs[0]: strs[1]}
for key, value in zip(strs[2::2], strs[3::2]):
    if key == "time":
        records.append(record)
        record = {key: value}
    else:
        record[key] = value
else:
    records.append(record)
    
table = pd.DataFrame(records)

Result:

     time     a     b     c    d
0  stamp1     1     2     3  NaN
1  stamp2    11    22   NaN    4
2  stamp3   111   222   333  NaN
3  stamp4  1111  2222  3333  444

Or do it via a generator:

import pandas as pd

def records(lst):
    record = {lst[0]: lst[1]}
    for key, value in zip(lst[2::2], lst[3::2]):
        if key == "time":
            yield record
            record = {key: value}
        else:
            record[key] = value
    else:
        yield record

table = pd.DataFrame(records(strs))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I sort the elements of an HashMap according to their values?

How to create a table with name as parameter?

How to create table dynamically according to data in an array

How to pass the table name in parameter in cassandra python

Suppose the name of a column in a dataframe is unknown to me, how can I sort the df according to the values in that column?

How to sort a TStringList according to it's values in Delphi 7

How to sort an SQL table according to an average value in another table

python: how can I sort elements within glob.list according to the name?

Sort cards according to its suite and values using python

How to sort students list according to their Marks using Insertion sort in Python

How to sort the list values according to the first letter of the value in Python

How to sort extracted values according to element conditions

How to sort html elements according to array values

How to sort a list according to another list? Python

How can I sort a Map according to the parameters of its values?

sort particular column values according to specific column python dataframe

How to sort table according to multiple columns at a time?

How to sort the values of an array according to the value of another array

How to change python code to create multiple values for one parameter?

how to sort a dataframe according to another dataframe column values?

How to sort google-sheet table according to version number column

How to sort gsheet table according to a custom sort function?

how to create the temporary table in snowflake procedure with procedure parameter in table name

How to sort table according to id

how to sort the string according to the integer values

How to sort values in pivot table by values names

How to pass a string of column name as a parameter into a CREATE TABLE FUNCTION in BigQuery

How to sort Pivot Table with values?

How to create a function to remove specific dataframe rows according to a predefined parameter in python