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.
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments