pandas Sorting Variables

Question:

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:

               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)
Asked By: user385411

||

Answers:

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
Answered By: abokey

the code now looks good and works. The following problem still exists.

If a number Example: 89410211684405105500 is not found, I would still like to write it to the file. At the moment he simply omits the ones that weren’t found.

It should look like this:

Nummer "not found" ICCID 89410211684405105500

code:

# !/usr/bin/env python3
import pandas as pd
import tkinter as tk
from tkinter import filedialog
import xlsxwriter


def main():
    path = 'C:\Users\test\Desktop\'
    alle_nummern = path + 'alle_08.12.2022.xlsx'

    wb = xlsxwriter.Workbook(path + 'output.xlsx')
    ws = wb.add_worksheet('Tabelle1')
    ws.set_column(0, 2, 22)
    format = wb.add_format({'num_format': '@'})

    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename()
    row = 1

    with open(file_path) as fp:
        Lines = fp.readlines()

        for line in Lines:
            lline = line.strip()
            df = pd.read_excel(alle_nummern, dtype=str)
            df_abc = df[df["ICCID"] == lline]

            for rows in df_abc.itertuples():
                print('Nummer', rows[2], 'ICCID', rows[1])
                ws.write_row(0, 0, ['Telefonnummer', 'ICCID'], format)
                ws.write_row(row, 0, [rows[2], rows[1]], format)
                row = row + 1

    wb.close()


if __name__ == '__main__':
    main()
Answered By: user385411
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.