Example code for pandas implementation by line selection

Time:2021-10-21
catalogue
  • 1. User defined row index
  • 2. Select data by common index
    • 2.1 select single line data by common index
    • 2.2 select multiple rows of data by row index
  • 3. Select data by location index
    • 3.2 select multiple rows of data by location index
  • 4. Select continuous multiple lines of data
    • 5. Select a line that meets the criteria
      • 5.1 single condition selection
      • 5.2 multiple condition selection
        • 5.2.1 multiple conditions are and
        • 5.2.2 multiple conditions are or relationships

    The excel tables used in this paper are as follows:

    1. User defined row index

    Dataframe is indexed by custom rows when reading excel tables. To show the effect, first customize the row index

    import pandas as pd
    ​
    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    Print ('before index setting: ')
    print(df)
    Print ('after index setting: ')
    DF. Index = ['one', 'two', 'three', 'four', 'five']
    print(df)

    result:
    Before setting the index:
       Area   province   city         time   Indicators     Address    Weight      character
    0   northeast   Liaoning   Dalian September 6, 2019   twelve   “123“   zero point seven eight   u”123″
    one   northwest   Guangdong   Xi’an September 7, 2019   eighty-seven   “124“   zero point six five   u”124″
    two   South China   Beijing   Shenzhen September 8, 2019   eighty-seven   “125“   zero point three four   u”125″
    three   North China   Hubei   Beijing September 9, 2019   forty-five   “126“   one point two three   u”126″
    four   Central China   Heilongjiang   Wuhan September 10, 2019   twenty-one   “127“   eight point nine zero   u”127″
    After setting the index:
       Area   province   city         time   Indicators     Address    Weight      character
    one   northeast   Liaoning   Dalian September 6, 2019   twelve   “123“   zero point seven eight   u”123″
    two   northwest   Guangdong   Xi’an September 7, 2019   eighty-seven   “124“   zero point six five   u”124″
    three   South China   Beijing   Shenzhen September 8, 2019   eighty-seven   “125“   zero point three four   u”125″
    four   North China   Hubei   Beijing September 9, 2019   forty-five   “126“   one point two three   u”126″
    five   Central China   Heilongjiang   Wuhan September 10, 2019   twenty-one   “127“   eight point nine zero   u”127″

    2. Select data by common index

    Here, the row ordinary index is actually the row name. For the convenience of writing, all subsequent indexes are called ordinary indexes.

    2.1 select single line data by common index

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    DF. Index = ['one', 'two', 'three', 'four', 'five']
    Print (DF. LOC ['I'])

    result:
    region                      northeast
    province                     Liaoning
    city                     Dalian
    Time    2019-09-06 00:00:00
    Indicators                     twelve
    Address                  “123“
    Weight                   zero point seven eight
    Character                 u”123″
    Name: I, dtype: object

    2.2 select multiple rows of data by row index

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    DF. Index = ['one', 'two', 'three', 'four', 'five']
    Print (DF. LOC [['one', 'three', 'four']])

    result:
       region   province   city         time   Indicators     Address    Weight      character
    one   northeast   Liaoning   Dalian September 6, 2019   twelve   “123“   zero point seven eight   u”123″
    three   south China   Beijing   Shenzhen September 8, 2019   eighty-seven   “125“   zero point three four   u”125″
    four   North China   Hubei   Beijing September 9, 2019   forty-five   “126“   one point two three   u”126″

    Note: when single column data is selected, the parameter is string type, and when multi column data is selected, the parameter is list type

    3. Select data by location index

    3.1 select single line data by location index

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    DF. Index = ['one', 'two', 'three', 'four', 'five']
    print(df.iloc[0])

    result:
    Area                     northeast
    province                     Liaoning
    city                      Dalian
    Time    2019-09-06 00:00:00
    Indicators                     twelve
    Address                  “123“
    Weight                   zero point seven eight
    Character                 u”123″
    Name: I, dtype: object

    3.2 select multiple rows of data by location index

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    DF. Index = ['one', 'two', 'three', 'four', 'five']
    print(df.iloc[[0, 1]])

    result:
       region   province   city         time   Indicators     Address    Weight      character
    one   northeast   Liaoning   Dalian September 6, 2019   twelve   “123“   zero point seven eight   u”123″
    two   northwest   Guangdong   Xi’an September 7, 2019   eighty-seven   “124“   zero point six five   u”124″

    4. Select continuous multiple lines of data

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    DF. Index = ['one', 'two', 'three', 'four', 'five']
    print(df.iloc[0:2])

    result:
       region   province   city         time   Indicators     Address    Weight      character
    one   northeast   Liaoning   Dalian September 6, 2019   twelve   “123“   zero point seven eight   u”123″
    two   northwest   Guangdong   Xi’an September 7, 2019   eighty-seven   “124“   zero point six five   u”124″

    It means to get the data from column 1 to column 3 of all rows. The syntax for selecting consecutive columns of data is similar to the slice syntax, so it is also called slice index.

    5. Select a line that meets the criteria

    5.1 single condition selection

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    Print (DF [DF ['indicator'] < 50])

    result:
       Area   province   city         time   Indicators    weight
    0   northeast   Liaoning   Dalian September 6, 2019   twelve   zero point seven eight
    three   North China   Hubei   Beijing September 9, 2019   forty-five   one point two three
    four   Central China   Heilongjiang   Wuhan September 10, 2019   twenty-one   eight point nine zero

    5.2 multiple condition selection

    5.2.1 multiple conditions are and

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    Print (DF [(DF ['index'] < 50) & (DF ['weight'] < 1)])

    result:
       region   province   city         time   Indicators    weight
    0   northeast   Liaoning   Dalian September 6, 2019   twelve   zero point seven eight

    5.2.2 multiple conditions are or relationships

    df = pd.read_excel(r'C:\Users\admin\Desktop\data_test.xlsx')
    Print (DF [(DF ['index'] < 50) | (DF ['weight'] < 1)])

    result:
       Area   province   city         time   Indicators    weight
    0   northeast    Liaoning   Dalian September 6, 2019   twelve   zero point seven eight
    one   northwest   Guangdong   Xi’an September 7, 2019   eighty-seven   zero point six five
    two   South China   Beijing   Shenzhen September 8, 2019   eighty-seven   zero point three four
    three   North China   Hubei   Beijing September 9, 2019   forty-five   one point two three
    four   Central China   Heilongjiang   Wuhan September 10, 2019   twenty-one   eight point nine zero

    This is the end of this article about the example code of pandas to select by line. For more information about pandas to select by line, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

    Recommended Today

    SQL statement of three-level linkage of provinces, cities and counties

    The first is the table creation statement Copy codeThe code is as follows: CREATE TABLE `t_address_province` ( `id` INT AUTO_ Increment primary key comment ‘primary key’,`Code ` char (6) not null comment ‘province code’,`Name ` varchar (40) not null comment ‘province name’)Engine = InnoDB default charset = utf8 comment = ‘province information table’; CREATE TABLE […]