Pandas Divide dataframe by index values

cmf05

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.

EdChum

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get row-index values of Pandas DataFrame as list?

From Java

pandas dataframe groupby index and convert row values into columns

From Java

Pandas Dataframe row selection combined condition index- and column values

From Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

From Dev

Pivoting pandas DataFrame -- AssertionError: Index length did not match values

From Dev

Pandas Dataframe - Count values based on index position

From Dev

Divide entire pandas multiIndex dataframe by dataframe variable

From Dev

Using a Pandas dataframe index as values for x-axis in matplotlib plot

From Dev

Retrieving .loc index values in pandas dataframe

From Dev

how to use values of a pandas DataFrame as numpy array index

From Dev

Slice Pandas dataframe by index values that are (not) in a list

From Dev

Pivoting a pandas dataframe with duplicate index values

From Dev

Divide One Pandas Dataframe by Another - Ignore index but respect columns

From Dev

Pandas DataFrame - Combining one column's values with same index into list

From Dev

Subset Pandas DataFrame Secondary Index and Reassigning values

From Dev

Pandas dataframe: slicing column values using second column for slice index

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

Getting the index of pandas dataframe for matching row values

From Dev

Is there an easier way to change the index values of a pandas dataframe?

From Dev

Summing values from pandas dataframe columns depending on row index value

From Dev

Pandas multi index dataset divide

From Dev

pandas dataframe: divide and replace

From Dev

Pandas divide fill with multiple values

From Dev

How to exchange the role of values and index in pandas DataFrame?

From Dev

Join pandas DataFrame values by closest index

From Dev

How to set index values in a MultiIndex pandas DataFrame?

From Dev

pandas series or tidy dataframe: index level values to dataframe columns

From Dev

Pandas: divide dataframe to some parts

From Dev

Use index values as category values in pandas dataframe

Related Related

  1. 1

    Get row-index values of Pandas DataFrame as list?

  2. 2

    pandas dataframe groupby index and convert row values into columns

  3. 3

    Pandas Dataframe row selection combined condition index- and column values

  4. 4

    Construct Pandas DataFrame from dictionary in form {index: list of row values}

  5. 5

    Pivoting pandas DataFrame -- AssertionError: Index length did not match values

  6. 6

    Pandas Dataframe - Count values based on index position

  7. 7

    Divide entire pandas multiIndex dataframe by dataframe variable

  8. 8

    Using a Pandas dataframe index as values for x-axis in matplotlib plot

  9. 9

    Retrieving .loc index values in pandas dataframe

  10. 10

    how to use values of a pandas DataFrame as numpy array index

  11. 11

    Slice Pandas dataframe by index values that are (not) in a list

  12. 12

    Pivoting a pandas dataframe with duplicate index values

  13. 13

    Divide One Pandas Dataframe by Another - Ignore index but respect columns

  14. 14

    Pandas DataFrame - Combining one column's values with same index into list

  15. 15

    Subset Pandas DataFrame Secondary Index and Reassigning values

  16. 16

    Pandas dataframe: slicing column values using second column for slice index

  17. 17

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  18. 18

    Getting the index of pandas dataframe for matching row values

  19. 19

    Is there an easier way to change the index values of a pandas dataframe?

  20. 20

    Summing values from pandas dataframe columns depending on row index value

  21. 21

    Pandas multi index dataset divide

  22. 22

    pandas dataframe: divide and replace

  23. 23

    Pandas divide fill with multiple values

  24. 24

    How to exchange the role of values and index in pandas DataFrame?

  25. 25

    Join pandas DataFrame values by closest index

  26. 26

    How to set index values in a MultiIndex pandas DataFrame?

  27. 27

    pandas series or tidy dataframe: index level values to dataframe columns

  28. 28

    Pandas: divide dataframe to some parts

  29. 29

    Use index values as category values in pandas dataframe

HotTag

Archive