How do I get all the rows before a specific index in Pandas?

GiannisIordanou

I am reading an xlsx file and I want for every row to create columns based on the rows before.

import pandas as pd
import numpy as np

def get_total(x):
    name = x["NAME"]
    city = x["CITY"]
    index = x.index
    records = df[df.index < index) & (df["NAME"] == name) & (df["CITY"] == city)]
    return records.size[0]

data_filename = "data.xslx"
df = pd.read_excel(data_filename, na_values=["", " ", "-"])
df["TOTAL"] = df.apply(lambda x: get_total(x), axis=1)

The get_total function is a simple example of what I want to achieve.

I could use df.reset_index(inplace=True) to get the dataframe's index as a column. I think there must be a better way to get the index of a row.

EdChum

You can rewrite your function like this:

def get_total(x):
    name = x["NAME"]
    city = x["CITY"]
    index = x.name
    records df.loc[0:index]
    return records.loc[(records['NAME'] == name) & (records['CITY']==city)].size

the name attribute is the current row index value

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

how do I insert a column at a specific column index in pandas?

From Java

How do I get the key at a specific index from a Dictionary in Swift?

From Dev

How do I get the name of the rows from the index of a data frame?

From Dev

If I have a collection of superclass type, how do I get all the items of a specific subclass type?

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

pandas: How do I select rows based on the sum of all columns?

From Dev

How to get the index of space before specific string?

From Dev

Get number of rows before and after a certain index value in pandas

From Dev

Pandas get specific rows from HDF5 by index

From Dev

How do I get the index of each item in a groupby object in Pandas?

From Dev

Dataframe split before a specific string for all rows

From Dev

How do I get the list of all terms in a Whoosh index?

From Dev

How do I get all the rows from the table SQLite?

From Dev

Get n rows before specific value in pandas

From Dev

How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

From Dev

Dataframe split before a specific string for all rows

From Dev

If I have a collection of superclass type, how do I get all the items of a specific subclass type?

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

How do I delete all text before and after a specific naming convention appears in Notepad++

From Dev

How do I get all the rows from the table SQLite?

From Dev

How do i get column values of all checked rows?

From Dev

How can I get index of rows in pandas?

From Dev

How do i select all rows that meet the value for a specific column in a specific row

From Dev

How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

From Dev

pandas dataframe imported CSV with all rows in one column. How do I fix this?

From Dev

How do I get the IDs of everybody with specific rows/values in MySQL

From Dev

How do I group rows in pandas?

From Dev

How can I use pandas to set a value for all rows that match part of a multi part index

From Dev

C# - How to get all rows back in dataGridView after reopening form ,but I applied filter before closing form

Related Related

  1. 1

    how do I insert a column at a specific column index in pandas?

  2. 2

    How do I get the key at a specific index from a Dictionary in Swift?

  3. 3

    How do I get the name of the rows from the index of a data frame?

  4. 4

    If I have a collection of superclass type, how do I get all the items of a specific subclass type?

  5. 5

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  6. 6

    pandas: How do I select rows based on the sum of all columns?

  7. 7

    How to get the index of space before specific string?

  8. 8

    Get number of rows before and after a certain index value in pandas

  9. 9

    Pandas get specific rows from HDF5 by index

  10. 10

    How do I get the index of each item in a groupby object in Pandas?

  11. 11

    Dataframe split before a specific string for all rows

  12. 12

    How do I get the list of all terms in a Whoosh index?

  13. 13

    How do I get all the rows from the table SQLite?

  14. 14

    Get n rows before specific value in pandas

  15. 15

    How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

  16. 16

    Dataframe split before a specific string for all rows

  17. 17

    If I have a collection of superclass type, how do I get all the items of a specific subclass type?

  18. 18

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  19. 19

    How do I delete all text before and after a specific naming convention appears in Notepad++

  20. 20

    How do I get all the rows from the table SQLite?

  21. 21

    How do i get column values of all checked rows?

  22. 22

    How can I get index of rows in pandas?

  23. 23

    How do i select all rows that meet the value for a specific column in a specific row

  24. 24

    How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

  25. 25

    pandas dataframe imported CSV with all rows in one column. How do I fix this?

  26. 26

    How do I get the IDs of everybody with specific rows/values in MySQL

  27. 27

    How do I group rows in pandas?

  28. 28

    How can I use pandas to set a value for all rows that match part of a multi part index

  29. 29

    C# - How to get all rows back in dataGridView after reopening form ,but I applied filter before closing form

HotTag

Archive