logo
Tags down

shadow

How can i multiply all columns next to each other in a pandas dataframe?


By : cole
Date : October 16 2020, 06:10 AM
wish helps you A common trick in pandas is grouping by every n values using floor division. Do this along axis=1 to get your desired output.
code :
n = 2

df.groupby(np.arange(df.shape[1]) // n, axis=1).prod()
   0   1  2
0  0   6  4
1  2  12  8
2  3   2  2


Share : facebook icon twitter icon

Multiply all columns in a Pandas dataframe together


By : user6177004
Date : March 29 2020, 07:55 AM
seems to work fine You could use DataFrame.prod():
code :
>>> df = pd.DataFrame(np.random.randint(1, 10, (5, 3)))
>>> df
   0  1  2
0  7  7  5
1  1  8  6
2  4  8  4
3  2  9  5
4  3  8  7
>>> df.prod(axis=1)
0    245
1     48
2    128
3     90
4    168
dtype: int64
>>> df = pd.DataFrame(np.random.randint(1, 10, (5, 3)))
>>> df
   0  1  2
0  9  3  3
1  8  5  4
2  3  6  7
3  9  8  5
4  7  1  2
>>> df.apply(np.prod, axis=1)
0     81
1    160
2    126
3    360
4     14
dtype: int64

How to selectively multiply or add columns in pandas DataFrame?


By : Les Hankel
Date : March 29 2020, 07:55 AM
hop of those help? Just to throw out an alternative (you can use the prod DataFrame method):
code :
In [11]: df['multiplied'] = df[df != 0].prod(axis=1)

In [12]: df
Out[12]: 
          P1    P2    P3  multiplied
Sample                              
A       0.25  0.50  0.25     0.03125
B       0.25  0.00  0.25     0.06250
C       0.25  0.25  0.00     0.06250

Multiply list of columns in pandas DataFrame


By : Rou Mass
Date : March 29 2020, 07:55 AM
hop of those help? Select the columns in the list using loc and then use .prod() across the rows (by specifying axis=1). For example:
code :
>>> cols = ['A','B','C','D']
>>> df = pd.DataFrame(np.random.randint(1, 4, size=(3,4)), columns=list('ABCD'))
>>> df
   A  B  C  D
0  2  2  1  1
1  2  3  1  1
2  3  1  3  3

>>> df['VAL'] = df.loc[:, cols].prod(axis=1)
>>> df
   A  B  C  D  VAL
0  2  2  1  1    4
1  2  3  1  1    6
2  3  1  3  3   27

Multiply two pandas dataframe with similar columns


By : LeonD
Date : March 29 2020, 07:55 AM
Hope that helps It looks like you want to perform a dot product on the values. This should be pretty fast -
code :
r = df_1.values.dot(df_2_t.values.T)

How can I multiply each column with the other columns of the Pandas DataFrame?


By : Darren Morris
Date : March 29 2020, 07:55 AM
should help you out The following is a brute force method, but it should do the job. permutations() generates all the column permutations. The inner sorted() together with set() merges ('A','B') with ('B','A'), etc.
Related Posts Related Posts :
  • Python hex string encoding
  • Get week start date from week number
  • How to use imports from requirements.txt in python
  • Removing tab indent in ipython shell
  • I need to remove duplicates from a list but add the numeric value in them
  • Delay default arguments being read until function is called
  • Interpolate / fillna with a decay formula in pandas
  • What python package can translate Greek letter to ASCII requivalent?
  • How to get output of OS command from Jupyter notebook?
  • Printing AND writing the RIGHTLY formatted number
  • How do I create a shortcut to import most used python modules?
  • Matplotlib: Show selected date labels on x axis
  • Understanding memoization in Python
  • why does the len function return 2 on some iterations when they are all the same length?
  • Change in preference value does not affect the results of Affinity propagation Clustering
  • returning values inside a function
  • Why cant I use a variable in str slicing?
  • Section divider in Spyder
  • Conditional statement in selenium if element does not exists
  • Pandas : how to select index/row label in dataframe that matches a condition
  • What does zero do in A[0] in this code? Why not empty or another number?
  • Google App Engine urlfetch PayloadTooLargeError: Request exceeds 10 MiB limit for URL
  • Is there a way to set up optional arguments to bypass input arguments?
  • Suppress OpenMP debug messages when running Tensorflow on CPU
  • How to do GridSearchCV for F1-score in classification problem with scikit-learn?
  • Why does .pop() eventually stop and not keep removing items from a list until the list is empty?
  • How do I acess my Spider data from my main.py script?
  • Python Pandas Expand a Column of List of Lists to Two New Column
  • Overhead of python multiprocessing initialization is worse than benefits
  • Python Joining List and adding and removing characters
  • Adding an lxml library to project
  • Concatenating tensors in Tensorflow with None axis
  • Need help understanding why i get attribute error
  • How to force a MIDI device to report control status?
  • What does *** mean in Python -3?
  • How to get GFCC instead of MFCC in python?
  • How do I print a number n times in python?
  • How do i split a string wherever there are digits?
  • List Comprehension Python Prime numbers
  • "list index out of range" when reading data from file
  • What's the correct datetime format for the specified date string?
  • I cannot import CSV file?
  • Matplotlib pyplot plots look different after calling pandas profiling. How can I fix this?
  • Stopping all the instances of a specific region
  • Deal with Birtish summer time
  • Unable to use ColorWheel without loading kv (AttributeError)
  • What are these characters called: 。. !?etc Trying to split sentences stops working with non standard characters
  • rand.randint returning same number over and over?
  • Find longest sequence that does not contain a certain number
  • How do I convert a map object to list and also assign to a variable
  • sympy error: 'Symbol' object has no attribute 'pi'
  • How to remove words without vowels from a list in python
  • Downloading python to macbook
  • TypeError: __init__() missing 1 required positional argument: 'units'
  • Check if a class is a dataclass in Python
  • Unable to scrape google news heading via their class
  • Array of structs with dynamic allocation runs very slow in C in comparison to Python
  • Python Pandas - find all unique combinations of rows of a DataFrame without repeating values in the columns
  • How do I change the numbers in a cell to the word 'Bus' in Pandas Python
  • 'ascii' codec can't encode character : ordinal not in range (128)
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk