Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas Sorting Variables

I am looking for the corresponding phone number from an Excel file from a list of data.

# !/usr/bin/env python3
import pandas as pd

def main():
    path = 'C:\\Users\\test\\Desktop\\'

    with open(path + 'list.txt') as fp:
        Lines = fp.readlines()
        for line in Lines:
            lline = line.strip()
            df = pd.read_excel(path + 'alle_08.12.2022.xlsx')
            df_abc = df[df["ICCID"] == lline]
            print(df_abc)

if __name__ == '__main__':
    main()

It works so far everything, I just want to adjust the output ICCID and phone number should be a variable

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

               ICCID     Nummer
66  89410211684401105500  763028100
               ICCID     Nummer
67  89410211684402105500  763028200
               ICCID     Nummer
68  89410211684403105500  763028300
               ICCID     Nummer
69  89410211684404105500  763028400
               ICCID     Nummer
70  89410211684405105500  763028500


Example Output
Nummer 763028100 ICCID 89410211684401105500
Nummer 763028200 ICCID 89410211684402105500
Nummer 763028300 ICCID 89410211684403105500
Nummer 763028400 ICCID 89410211684404105500
Nummer 763028500 ICCID 89410211684405105500

print('Nummer' + Nummer, 'ICCID' + ICCID)

>Solution :

You can use pandas.DataFrame.itertuples or any other similar function :

for row in df_abc.itertuples():
    print('Nummer', row[2], 'ICCID', row[1])

# Output :

Nummer 763028100 ICCID 89410211684401105500
Nummer 763028200 ICCID 89410211684402105500
Nummer 763028300 ICCID 89410211684403105500
Nummer 763028400 ICCID 89410211684404105500
Nummer 763028500 ICCID 89410211684405105500

Alternative (more efficient) :

You can concatenate the two columns and make a single one and then use pandas.Series.to_string:

print(('Nummer ' + df_abc['Nummer'].astype(str) + 'ICCID ' + df_abc['ICCID'].astype(str)).to_string())

66    Nummer 763028100ICCID 89410211684401105500
67    Nummer 763028200ICCID 89410211684402105500
68    Nummer 763028300ICCID 89410211684403105500
69    Nummer 763028400ICCID 89410211684404105500
70    Nummer 763028500ICCID 89410211684405105500
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading