Creating a dataframe in pandas by multiplying two series together

2024/9/29 17:25:37

Say I have two series in pandas, series A and series B. How do I create a dataframe in which all of those values are multiplied together, i.e. with series A down the left hand side and series B along the top. Basically the same concept as this, where series A would be the yellow on the left and series B the yellow along the top, and all the values in between would be filled in by multiplication:

http://www.google.co.uk/imgres?imgurl=http://www.vaughns-1-pagers.com/computer/multiplication-tables/times-table-12x12.gif&imgrefurl=http://www.vaughns-1-pagers.com/computer/multiplication-tables.htm&h=533&w=720&sz=58&tbnid=9B8R_kpUloA4NM:&tbnh=90&tbnw=122&zoom=1&usg=__meqZT9kIAMJ5b8BenRzF0l-CUqY=&docid=j9BT8tUCNtg--M&sa=X&ei=bkBpUpOWOI2p0AWYnIHwBQ&ved=0CE0Q9QEwBg

Sorry, should probably have added that my two series are not the same length. I'm getting an error now that 'matrices are not aligned' so I assume that's the problem.

Answer

You can use matrix multiplication dot, but before you have to convert Series to DataFrame (because dot method on Series implements dot product):

>>> B = pd.Series(range(1, 5))
>>> A = pd.Series(range(1, 5))
>>> dfA = pd.DataFrame(A)
>>> dfB = pd.DataFrame(B)
>>> dfA.dot(dfB.T)0  1   2   3
0  1  2   3   4
1  2  4   6   8
2  3  6   9  12
3  4  8  12  16
https://en.xdnf.cn/q/71190.html

Related Q&A

UnicodeDecodeError in PyCharm debugger

Its a reference to UnicodeDecodeError while using cyryllic .I have same problem with Python 3.3 and Pycharm 2.7.2 Tryed to hardcode encoding in code, manually specifying encoding in Pycharm options, bu…

Scipy griddata with linear and cubic yields nan

the following code should produce griddata. But in case I choose as interpolation type cubic or linear I am getting nans in the z grid. Wen im choosing nearest everything is running fine. Here is an ex…

Clone a module and make changes to the copy

Is it possible to copy a module, and then make changes to the copy? To phrase another way, can I inherit from a module, and then override or modify parts of it?

AWS Lambda, Python, Numpy and others as Layers

I have been going at this for a while trying to get python, numpy and pytz added to AWS Lambda as Layers rather than having to zip and throw it at AWS with my .py file. I was able to follow multiple tu…

Is there a way to check if a module is being loaded by multiprocessing standard module in Windows?

I believe on Windows, because there is no fork, the multiprocessing module reloads modules in new Pythons processes. You are required to have this code in your main script, otherwise very nasty crashes…

Condas solving environment takes forever

I am using conda since one year, since several weeks, whenever I want to install a package using conda install -c anaconda <package_name>, for any package, it is just stuck at the Solving environ…

How to overwrite a file in Python?

Im trying to overwrite a file. I based my answer on this Read and overwrite a file in PythonTo complete my codes:<select class="select compact expandable-list check-list" ONCHANGE="lo…

Is os.popen really deprecated in Python 2.6?

The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance:>>> import os >>> [c.close() for c in os…

Routes with trailing slashes in Pyramid

Lets say I have a route /foo/bar/baz. I would also like to have another view corresponding to /foo or /foo/. But I dont want to systematically append trailing slashes for other routes, only for /foo a…

understanding item for item in list_a if ... PYTHON

Ive seen the following code many times, and I know its the solution to my problems, but Im really struggling to understand HOW it works. The code in particular is:item for item in list_a if item not in…