output the data on request

Question:

Good afternoon here. Please help with the start (algorithm) of the program.
I have an excel file that has columns with the names of students, their last names, their phone numbers, what country they are from and their marks.

How can I write a python program with which I can input something of the data (phone number, name or surname) and I have to output all the data about the student, all that is known about him. And preferably in a separate file (excel, csv). Thanks for the help.

excel file:

Name Last Name Phone number Country Mark
Mike Jackson 5534987 USA A
Ani Ward 4567456 UK A
Alex Sid 7745879 France C

If anything, I don’t know how to use json and sql. But if it will be easier, than ok.

I entering: 7745879
Output:
| Alex | Sid | 7745879 | France | C |

My code only reads the file. I don’t know what to do next.

import pandas as pd
import os
file = 'Students.xlsx'

x1=pd.ExcelFile(file)

df1=x1.parse('Math')

for line in df1:
  print(df1.loc[:, 'Phone number'])
Asked By: Katli

||

Answers:

You could check across all the entries in each row of your DataFrame to see if any value matches with your input –

inp = input("Enter something:t")
# Enter something:        7745879
out_df = pd.DataFrame([rec for rec in df.values if any([inp == str(val) for val in rec])], columns=df.columns)

The list comprehension is doing the same thing as the following for loops – which are easier to understand –

for rec in df.values:
    for val in rec:
        if inp == str(val):
            out_df = pd.DataFrame([rec for rec in df.values if any([inp == str(val) for val in rec])], columns=df.columns)

Output

   Name Last Name  Phone number Country Mark
0  Alex       Sid       7745879  France    C

You can then write this out_df to an excel with something like out_df.to_excel(f'output_for_{inp}.xlsx')

Answered By: Mortz
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.