I think I would do this with two groupbys.
First to calculate the “weighted average”:
In [11]: g = df.groupby('Date') In [12]: df.value / g.value.transform("sum") * df.wt Out[12]: 0 0.125000 1 0.250000 2 0.416667 3 0.277778 4 0.444444 dtype: float64
If you set this as a column, you can groupby over it:
In [13]: df['wa'] = df.value / g.value.transform("sum") * df.wt
Now the sum of this column is the desired:
In [14]: g.wa.sum() Out[14]: Date 01/01/2012 0.791667 01/02/2012 0.722222 Name: wa, dtype: float64
or potentially:
In [15]: g.wa.transform("sum") Out[15]: 0 0.791667 1 0.791667 2 0.791667 3 0.722222 4 0.722222 Name: wa, dtype: float64
Related Posts:
- ValueError: Unknown label type: ‘continuous’
- How to fix IndexError: invalid index to scalar variable
- Convert pandas dataframe to NumPy array
- ImportError: Missing required dependencies [‘numpy’]
- Python Pandas – Missing required dependencies [‘numpy’] 1
- ‘DataFrame’ object has no attribute ‘sort’
- ‘DataFrame’ object has no attribute ‘sort’
- TypeError: cannot unpack non-iterable int objec
- ‘DataFrame’ object has no attribute ‘sort’
- What is dtype(‘O’), in pandas?
- What is dtype(‘O’), in pandas?
- TypeError: ‘DataFrame’ object is not callable
- ValueError: ‘object too deep for desired array’
- What does axis in pandas mean?
- How to take column-slices of dataframe in pandas
- Normalize data in pandas
- Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
- pandas create new column based on values from other columns / apply a function of multiple columns, row-wise
- Building multi-regression model throws error: `Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).`
- Coalesce values from 2 columns into a single column in a pandas dataframe
- Concat DataFrame Reindexing only valid with uniquely valued Index objects
- Replacing Pandas or Numpy Nan with a None to use with MysqlDB
- Difference between data type ‘datetime64[ns]’ and ‘
- Merging two DataFrames
- vectorize conditional assignment in pandas dataframe
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Import Error: No module named numpy
- Writing a pandas DataFrame to CSV file
- Difference between import numpy and import numpy as np
- ImportError: No module named pandas
- How to change the order of DataFrame columns?
- Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?
- ImportError: No module named pandas. Pandas installed pip
- How can I use the apply() function for a single column?
- Convenient way to deal with ValueError: cannot reindex from a duplicate axis
- “Series objects are mutable and cannot be hashed” error
- Converting list to numpy array
- How do I read CSV data into a record array in NumPy?
- How to normalize a NumPy array to a unit vector?
- DataFrame constructor not properly called! error
- How to count the occurrence of certain item in an ndarray?
- size of NumPy array
- pandas DataFrame “no numeric data to plot” error
- Convert a tensor to numpy array in Tensorflow?
- Use a.any() or a.all()
- Most efficient way to map function over numpy array
- Get total of Pandas column
- Type error: cannot convert the series to
- How to check whether a pandas DataFrame is empty?
- Convert columns to string in Pandas
- Cannot convert list to array: ValueError: only one element tensors can be converted to Python scalars
- How can I use numpy.correlate to do autocorrelation?
- Change column type in pandas
- Calculating Covariance with Python and Numpy
- How to access the ith column of a NumPy multidimensional array?
- Dump a NumPy array into a csv file
- Python: find position of element in array
- Inverse of a matrix using numpy
- How to convert a PIL Image into a numpy array?
- Python: slicing a multi-dimensional array
- Python: pandas merge multiple dataframes
- How to count the NaN values in a column in pandas DataFrame
- Pandas – DataFrame object is not callable
- Convert pandas data frame to series
- How do you get the magnitude of a vector in Numpy?
- How to calculate rolling / moving average using python + NumPy / SciPy?
- Converting strings to floats in a DataFrame
- Get first row value of a given column
- ValueError: object too deep for desired array while using convolution
- Make list of arrays in python
- Import Error: No module named numpy Anaconda
- How to add an empty column to a dataframe?
- How to apply a function to two columns of Pandas dataframe
- Convert categorical data in pandas dataframe
- How to fix “TypeError: len() of unsized object”
- OSError: Initializing from file failed on csv in Pandas
- Insert a row to pandas dataframe
- How to save a Seaborn plot into a file
- How to get the unit vector from a numpy array
- Subtract two columns in dataframe
- pandas DataFrame: replace nan values with average of columns
- Pandas error “Can only use .str accessor with string values”
- Efficiently sorting a numpy array in descending order?
- Rename specific column(s) in pandas
- How to create a numpy array of lists?
- How to convert column with dtype as object to string in Pandas Dataframe
- How to fix ‘RuntimeWarning: divide by zero encountered in double_scalars’
- IndexError: index 2 is out of bounds for axis 0 with size 2
- Pandas: sum up multiple columns into one column without last column
- built-in range or numpy.arange: which is more efficient?
- Move column by name to front of table in pandas
- How to print the full NumPy array, without truncation?
- How to install NumPy for Python 3.6
- Complex number troubles with numpy
- ValueError: multiclass format is not supported
- Filtering a NumPy Array
- TypeError: zip argument #2 must support iteration
- CS231n: How to calculate gradient for Softmax loss function?
- How to zip two 1d numpy array to 2d numpy array [duplicate]
- numpy/scipy/ipython:Failed to interpret file as a pickle