I am trying to divide all columns in the dataframe by the index.(1221 rows, 1000 columns)
5000058004097 5000058022936 5000058036940 5000058036827 \
91.0 3.667246e+10 3.731947e+12 2.792220e+14 2.691262e+13
94.0 9.869027e+10 1.004314e+13 7.514220e+14 7.242529e+13
96.0 2.536914e+11 2.581673e+13 1.931592e+15 1.861752e+14
...
Here is the code I have tried...
A = SHIGH.divide(SHIGH.index, axis =1)
and I get this error:
ValueError: operands could not be broadcast together with shapes (1221,1000) (1221,)
I have also tried
A = SHIGH.divide(SHIGH.index.values.tolist(), axis =1)
and also reindexing and using the column to divide and get the same error.
If someone could please point out my mistake it would be much appreciated.
div() method divides element-wise division of one pandas DataFrame by another. DataFrame elements can be divided by a pandas series or by a Python sequence as well. Calling div() on a DataFrame instance is equivalent to invoking the division operator (/).
In the above example, the data frame 'df' is split into 2 parts 'df1' and 'df2' on the basis of values of column 'Weight'. Method 2: Using Dataframe. groupby(). This method is used to split the data into groups based on some criteria.
Pandas DataFrame reset_index() Method The reset_index() method allows you reset the index back to the default 0, 1, 2 etc indexes. By default this method will keep the "old" idexes in a column named "index", to avoid this, use the drop parameter.
In the pandas series constructor, the div() or divide() method is used to perform element-wise floating division operation between the two series objects or between a series and a scalar. The method returns a series with resultant floating division values.
You need to convert the Index
object to a Series
:
df.div(df.index.to_series(), axis=0)
Example:
In [118]:
df = pd.DataFrame(np.random.randn(5,3))
df
Out[118]:
0 1 2
0 0.828540 -0.574005 -0.535122
1 -0.126242 2.152599 -1.356933
2 0.289270 -0.663178 -0.374691
3 -0.016866 -0.760110 -1.696402
4 0.130580 -1.043561 0.789491
In [124]:
df.div(df.index.to_series(), axis=0)
Out[124]:
0 1 2
0 inf -inf -inf
1 -0.126242 2.152599 -1.356933
2 0.144635 -0.331589 -0.187345
3 -0.005622 -0.253370 -0.565467
4 0.032645 -0.260890 0.197373
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With