Pandas Divide dataframe by index values

Related searches

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

pandas.DataFrame.divide — pandas 0.25.0 documentation, Get Floating division of dataframe and other, element-wise (binary operator Broadcast across a level, matching Index values on the passed MultiIndex level. pandas.DataFrame.divide¶ DataFrame.divide (other, axis = 'columns', level = None, fill_value = None) [source] ¶ Get Floating division of dataframe and other, element-wise (binary operator truediv). Equivalent to dataframe / other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rtruediv.

Another way of doing this is

df.div(df.index.values, axis=0)

Example:

In [7]: df = pd.DataFrame({'a': range(5), 'b': range(1, 6), 'c': range(2, 7)}).set_index('a')

In [8]: df.divide(df.index.values, axis=0)
Out[8]: 
          b         c
a                    
0       inf       inf
1  2.000000  3.000000
2  1.500000  2.000000
3  1.333333  1.666667
4  1.250000  1.500000

Pandas Divide dataframe by index 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� Divide a DataFrame in a ratio; Python | Pandas DataFrame.fillna() to replace Null values in dataframe; Convert given Pandas series into a dataframe with its index as another column on the dataframe; Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array; Python | Pandas Series.divide() Add a row at top in pandas DataFrame; Get the specified row value of a given Pandas DataFrame

You need convert index to_series and then divide by div:

print (SHIGH.divide(SHIGH.index.to_series(), axis = 0))
      5000058004097  5000058022936  5000058036940  5000058036827
91.0   4.029941e+08   4.101041e+10   3.068374e+12   2.957431e+11
94.0   1.049896e+09   1.068419e+11   7.993851e+12   7.704818e+11
96.0   2.642619e+09   2.689243e+11   2.012075e+13   1.939325e+12

In both solutions timings are same:

SHIGH = pd.DataFrame({'5000058022936': {96.0: 25816730000000.0, 91.0: 3731947000000.0, 94.0: 10043140000000.0}, 
                 '5000058036940': {96.0: 1931592000000000.0, 91.0: 279222000000000.0, 94.0: 751422000000000.0}, 
                 '5000058036827': {96.0: 186175200000000.0, 91.0: 26912620000000.0, 94.0: 72425290000000.0}, 
                 '5000058004097': {96.0: 253691400000.0, 91.0: 36672460000.0, 94.0: 98690270000.0}})


print (SHIGH)
      5000058004097  5000058022936  5000058036827  5000058036940
91.0   3.667246e+10   3.731947e+12   2.691262e+13   2.792220e+14
94.0   9.869027e+10   1.004314e+13   7.242529e+13   7.514220e+14
96.0   2.536914e+11   2.581673e+13   1.861752e+14   1.931592e+15

#[1200 rows x 1000 columns] in sample DataFrame
SHIGH = pd.concat([SHIGH]*400).reset_index(drop=True)
SHIGH = pd.concat([SHIGH]*250, axis=1)

In [212]: %timeit (SHIGH.divide(SHIGH.index.values, axis = 0))
100 loops, best of 3: 14.8 ms per loop

In [213]: %timeit (SHIGH.divide(SHIGH.index.to_series(), axis = 0))
100 loops, best of 3: 14.9 ms per loop

Python, Pandas dataframe.div() is used to find the floating division of the dataframe level : Broadcast across a level, matching Index values on the� pandas.DataFrame.divide¶ DataFrame.divide (self, other, axis='columns', level=None, fill_value=None) [source] ¶ Get Floating division of dataframe and other, element-wise (binary operator truediv). Equivalent to dataframe / other, but with support to substitute a fill_value

SHIGH / SHIGH.index

df.index gives you an array-like structure which stores the indices.

Pandas Divide dataframe by index values, I am trying to divide all columns in the dataframe by the index.(1221 rows, 1000 columns) 5000058004097 5000058022936 5000058036940� Sometimes in order to analyze the Dataframe more accurately, we need to split it into 2 or more parts. The Pandas provide the feature to split Dataframe according to column index, row index, and column values, etc. Let’ see how to Split Pandas Dataframe by column value in Python

Dividing each row by the previous one, Pandas sum across columns and divide each cell from that value. 1. pandas. DataFrame.divide, For Series input, axis to match Series index on. levelint or label. pandas.Series.divide¶ Series.divide (other, level = None, fill_value = None, axis = 0) [source] ¶ Return Floating division of series and other, element-wise (binary operator truediv). Equivalent to series / other, but with support to substitute a fill_value for missing data in either one of the inputs. Parameters other Series or scalar value

Note: pandas supports non-unique index values. If an operation that does not support duplicate index values is attempted, an exception will be raised at that time. <data_frame>.append(other

pandas.DataFrame.set_index¶ DataFrame.set_index (keys, drop = True, append = False, inplace = False, verify_integrity = False) [source] ¶ Set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand