1
votes

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'])
1
Please provide a sample of your input as text. - Mortz
Or take a look at realpython.com/python-csv . - rajah9
and i want to input phone number (as example: 7745879 ) and to output all data about student, who have this number. Output (Alex Sid France 7745879 C) - Katli
That's great! It really helps to understand that you are using pandas dataframes to pull in the excel data. Can you share what that dataframe looks like (just like top 5 records in the DF). Likely your .loc() or similar filtering is the right path forward. - JNevill

1 Answers

0
votes

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')