1

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'])
Katli
  • 31
  • 4
  • Please provide a sample of your input as text. – Mortz Jul 28 '22 at 14:41
  • Or take a look at https://realpython.com/python-csv/ . – rajah9 Jul 28 '22 at 14:47
  • Does this answer your question? [How to write to an Excel spreadsheet using Python?](https://stackoverflow.com/questions/13437727/how-to-write-to-an-excel-spreadsheet-using-python) – rajah9 Jul 28 '22 at 14:47
  • 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 Jul 28 '22 at 14:59
  • @rajah9 thanks, but I need to understand the technique of how you can find all the data on a student by request.. For example, I made an input a phone number and I should get all the data about the owner of this number. – Katli Jul 28 '22 at 15:02
  • You need to understand a Python if statement. I suggest you look at existing programs on geeksforgeeks or realpython. – rajah9 Jul 28 '22 at 15:09
  • This is a lot of questions in one. You have the requirements/specs for an entire program and your question amounts to "How do I develop this program from these specs". It's wildly broad for stackoverflow. Instead concentrate on each individual problem. An appropriate question for this site would be: "If I have a string to search for in an excel worksheet (column D) how do I perform that search? Heres my attempt so far , but I'm stuck here " – JNevill Jul 28 '22 at 15:19
  • @JNevill ok, I published that code, but it doesn't make much sense. – Katli Jul 28 '22 at 15:27
  • 1
    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 Jul 28 '22 at 15:34
  • @JNevill thanks! 0 5534987 1 4567456 2 7745879 3 5634876 4 7755123 5 3499759 6 9934555 Name: Phone number, dtype: int64 that what I have in output – Katli Jul 28 '22 at 15:47

1 Answers1

2

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

Mortz
  • 4,654
  • 1
  • 19
  • 35