JMP0629
JMP0629

Reputation: 95

Python Pandas: Breaking a list or series into columns of different sizes

I have a single series with 2 columns that looks like

1   5.3
2   2.5
3   1.6
4   3.8
5   2.8

...and so on. I would like to take this series and break it into 6 columns of different sizes. So (for example) the first column would have 30 items, the next 31, the next 28, and so on. I have seen plenty of examples for same-sized columns but have not seen away to make multiple custom-sized columns.

Upvotes: 1

Views: 242

Answers (1)

Caio Belfort
Caio Belfort

Reputation: 555

Based on comments you can try use the index of the series to fill your dataframe

s = pd.Series([5, 2, 1, 3, 2])
df = pd.DataFrame([], index=s.index)
df['col1'] = s.loc[:2]
df['col2'] = s.loc[3:3]
df['col3'] = s.loc[4:]

Result:

    col1  col2  col3
0   5.0   NaN   NaN
1   2.0   NaN   NaN
2   1.0   NaN   NaN
3   NaN   3.0   NaN
4   NaN   NaN   4.0

Upvotes: 1

Related Questions