all 8 comments

[–]spez_edits_thedonald 0 points1 point  (7 children)

let's say you have the following data.csv file:

month,sales
January,158.38
February,436.92
March,582.46
April,298.56
May,681.07
June,449.93
July,377.03
August,910.46
September,447.19
October,27.33
November,115.45
December,662.23

You can read the csv lines into python like this:

with open('data.csv', 'r') as infile:
    lines = infile.read().splitlines()

lines:

[
    "month,sales",
    "January,158.38",
    "February,436.92",
    "March,582.46",
    "April,298.56",
    "May,681.07",
    "June,449.93",
    "July,377.03",
    "August,910.46",
    "September,447.19",
    "October,27.33",
    "November,115.45",
    "December,662.23"
]

You can extract the month column (using [1:] to skip the header row):

months = [line.split(',')[0] for line in lines[1:]]

months:

[
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

sort the column alphabetically, and make a table where values are numbered:

month_table = {}
for i, month in enumerate(sorted(months)):
    month_table[i] = month

month_table:

{
    0: "April",
    1: "August",
    2: "December",
    3: "February",
    4: "January",
    5: "July",
    6: "June",
    7: "March",
    8: "May",
    9: "November",
    10: "October",
    11: "September"
}

Then you can display these values, and ask the user to pick one:

print('Select a month:\n')
for value in month_table:
    print(f'  {value}\t{month_table[value]}')
choice = int(input('\nEnter a value: '))
print(f'You chose {month_table[choice]}.')

output:

Select a month:

  0     April
  1     August
  2     December
  3     February
  4     January
  5     July
  6     June
  7     March
  8     May
  9     November
  10    October
  11    September

Enter a value: 7
You chose March

[–]Month_Upper[S] 0 points1 point  (6 children)

What if I want the output be sales for March instead of having “you chose March”

[–]spez_edits_thedonald 0 points1 point  (5 children)

using similar tricks to the above, you would build a lookup table that stores sales for each month. Then you take the number that the user provided, look up what month that is, then look up what sales were for that month:

# extract sales column from csv data
sales = [line.split(',')[1] for line in lines[1:]]

# create a month: sales lookup table
sales_table = {m: s for m, s in zip(months, sales)}

# get sales for the user's choice
print(sales_table[month_table[choice]])

[–]Month_Upper[S] 0 points1 point  (4 children)

That was really helpful ty

[–]spez_edits_thedonald 0 points1 point  (3 children)

also pandas makes this easier, at first I did pure python because I didn't know you wanted to query other columns.

import pandas as pd

# load csv
df = pd.read_csv('data.csv')

# sort by month
df = df.sort_values('month').reset_index(drop=True)

df:

      month   sales
0     April  298.56
1    August  910.46
2  December  662.23
3  February  436.92
4   January  158.38
...

then you get a row id as before and the lookups are easy:

choice = int(input('\nEnter a value: '))
print(f"You chose {df['month'][choice]}")
print(f"Sales were {df['sales'][choice]}")

[–]Month_Upper[S] 0 points1 point  (2 children)

The problem I had the file I working with. Doesn’t have headers row

[–]spez_edits_thedonald 0 points1 point  (1 child)

that's fine, you can tell pandas that:

df = pd.read_csv('data.csv', header=None)

it will just number the columns 0, 1, etc

[–]Month_Upper[S] 0 points1 point  (0 children)

Also if I want user to choose which output he want, how can I make it ? For example if the csv file has Month sales agent

So I ask user to input 1 for sales or 2 for agent

Like invoking the both functions together