Convert list of repeated column names and list of values into a dataframe

Viewed 141

Let's say I have two lists. One contains a list of headers that is repeated. And one contains values. See example below:

cols = ['col1', 'col2', 'col3', 'col1', 'col2', 'col3']
values = [1, 3, 4, 6, 7, 1]

How would I create a dataframe from such a structure with three columns ('col1', 'col2', 'col3') and two rows.

Desired result

'col1', 'col2', 'col3'
1,3,4
6,7,1
4 Answers

You can try converting values to series, then groupby on cols and agg to list, finally convert to dataframe and transpose

s = pd.Series(values).groupby(cols).agg(list)
pd.DataFrame.from_dict(s.to_dict())

   col1  col2  col3
0     1     3     4
1     6     7     1

Or using defaultdict:

from collections import defaultdict
d = defaultdict(list)
for k,v in zip(cols,values):
        d[k].append(v)
pd.DataFrame.from_dict(d)

Assuming cols and values are simply repeated (making both structured lists with a set order and alignment operations are unneeded) we can simply convert values to an array and reshape based on the unique column values:

import pandas as pd
import numpy as np

cols = ['col1', 'col2', 'col3', 'col1', 'col2', 'col3']
values = [1, 3, 4, 6, 7, 1]

# Unique Col Values
u_cols = np.unique(cols)
# Build DataFrame
df = pd.DataFrame(
    np.array(values).reshape(-1, len(u_cols)),
    columns=u_cols
)

df:

   col1  col2  col3
0     1     3     4
1     6     7     1

Or as a csv:

df.to_csv('output.csv', index=False, quotechar="'", quoting=2)

output.csv:

'col1','col2','col3'
1,3,4
6,7,1

Try:

import csv


cols = ["col1", "col2", "col3", "col1", "col2", "col3"]
values = [1, 3, 4, 6, 7, 1]

df = (
    pd.DataFrame(zip(cols, values))
    .pivot(columns=0, values=1)
    .apply(sorted, key=pd.isna)
    .dropna()
)
print(df.to_csv(index=False, quotechar="'", quoting=csv.QUOTE_NONNUMERIC))

Prints:

'col1','col2','col3'
1.0,3.0,4.0
6.0,7.0,1.0

You can convert the things to a dict and then access the values from the keys

cols = ['col1', 'col2', 'col3', 'col1', 'col2', 'col3']
values = [1, 3, 4, 6, 7, 1]

the_dict = {
    key: [x[1] for x in zip(cols, values) if x[0] == key] 
    for key in set(cols)
}

print(the_dict)
Related