Say I have a predefined data frame of size NxM (N rows and M columns), it is initialized with all NaNs.
Now say I have a series of size M, I want to fill all rows with that series. For example, if the series is of ints [3,4,5] where N=2, M=3, my resultant dataframe should be (I have included column and row indices):
0 | 1 | 2 | |
---|---|---|---|
0 | 3 | 4 | 5 |
1 | 3 | 4 | 5 |
Alternatively, say I have a series of size N, I want to fill all columns with that series. For example, if the series is of ints [10, 11] where N=2, M=3, my resultant dataframe should be (I have included column and row indices):
0 | 1 | 2 | |
---|---|---|---|
0 | 10 | 10 | 10 |
1 | 11 | 11 | 11 |
How do I do both of these operations in pure pandas without resorting to using something like np.repeat
or np.tile
?
CodePudding user response:
You can use np.broadcast_to
, last if necessary transpose ouput for second sample data:
s = pd.Series([3,4,5])
N = 2
M = 3
df = pd.DataFrame(index=range(N), columns=range(M))
df[:] = np.broadcast_to(s.to_numpy(), (N, M))
print (df)
0 1 2
0 3 4 5
1 3 4 5
s = pd.Series([10, 11])
N = 2
M = 3
df = pd.DataFrame(index=range(N), columns=range(M))
df[:] = np.broadcast_to(s.to_numpy(), (M, N)).T
print (df)
0 1 2
0 10 10 10
1 11 11 11
s = pd.Series([3,4,5])
N = 2
M = 3
df = pd.DataFrame(np.broadcast_to(s.to_numpy(), (N, M)))
print (df)
0 1 2
0 3 4 5
1 3 4 5
s = pd.Series([10, 11])
N = 2
M = 3
df = pd.DataFrame(np.broadcast_to(s.to_numpy(), (M, N)).T)
print (df)
0 1 2
0 10 10 10
1 11 11 11