Guforu
Guforu

Reputation: 4023

Table, with the different length of columns

I have some list of items:

list = [1, 2, 3, 4, 5, ...... 2, 7, 4, 9, .....]

Now I need to create such table with the specific fix number of columns. My code should have next logic:

Some first solution is to create another lists and just put the values into the lists. At the end I can normalize the length of lists (for example with some values like NaN) and create from these lists DF.

Probably somebody can say me better solution...

For example I try to do it with the pandas DF, but I don't want add the whole row, just unique value into specific column.

Upvotes: 0

Views: 1030

Answers (1)

HYRY
HYRY

Reputation: 97331

You can use groupby.cumcount() and pivot(), here is an example:

import numpy as np
import pandas as pd
numbers = np.random.randint(0, 1000, 100)
col = numbers % 4 # put your logic here
s = pd.Series(numbers)
cnt = s.groupby(col).cumcount()
pd.pivot(cnt, col, numbers)

Upvotes: 1

Related Questions