This question already has an answer here:
What is the best way to multiply all the columns of a Pandas DataFrame
by a column vector stored in a Series
? I used to do this in Matlab with repmat()
, which doesn't exist in Pandas. I can use np.tile()
, but it looks ugly to convert the data structure back and forth each time.
Thanks.
This can be accomplished quite simply with the DataFrame method
apply
.Now that we have our
DataFrame
andSeries
we need a function to pass toapply
.We can pass this to
df.apply
and we are good to godf.apply
acts column-wise by default, but it can can also act row-wise by passingaxis=1
as an argument toapply
.This could be done more concisely by defining the anonymous function inside
apply
What's wrong with
?
http://pandas.pydata.org/pandas-docs/stable/basics.html#flexible-binary-operations
Why not create your own dataframe tile function:
Example:
However, the docs note: "DataFrame is not intended to be a drop-in replacement for ndarray as its indexing semantics are quite different in places from a matrix." Which presumably should be interpreted as "use numpy if you are doing lots of matrix stuff".