0 votes
in Python Pandas by
How to convert a numpy array to a dataframe of given shape?

1 Answer

0 votes
by

We can reshape the series p into a dataframe with 6 rows and 2 columns as below example:

import pandas as pd

import numpy as np

p = pd.Series(np.random.randint(1, 7, 35))

# Input

p = pd.Series(np.random.randint(1, 7, 35))

info = pd.DataFrame(p.values.reshape(7,5))

print(info)

Output:

0  1  2  3  4

0  3  2  5  5  1

1  3  2  5  5  5

2  1  3  1  2  6

3  1  1  1  2  2

4  3  5  3  3  3

5  2  5  3  6  4

6  3  6  6  6  5

Related questions

0 votes
asked Aug 7, 2021 in PyTorch by sharadyadav1986
0 votes
asked Nov 9, 2021 in Python Pandas by Robin
...