简体   繁体   中英

The Aim was to get the user to enter some new data sales and it would enter the data table what should I do?

import pandas as pd

headings = ["Item Name", "Cost Price", "Amount sold", "Profit"]
df = pd.read_table("/Users/arnav/SalesData_April2021-1.txt",
                sep =',',
                header = None,
                #columns = headings)
                names = headings)
df.head()
print(df)

print("enter an item name")
name1 = input()
print("cost price")
CostPrice = input()
print("number of sales")
NumberSales = input()
print("Profit Margin")
profitmargin = input()

print("enter another item name")
name2 = input()
print("cost price")
CostPrice2 = input()
print("number of sales")
NumberSales2 = input()
print("Profit Margin")
profitmargin2 = input()

df['Item Name'].append(str(name1))
df['Cost Price'].append(int(CostPrice1))
df['Amount Sold'].append(int(NumberSales1))
df['Profit'].append(int(profitmargin1))

df['Item Name'].append(str(name1))
df['Cost Price'].append(int(CostPrice1))
df['Amount Sold'].append(int(NumberSales1))
df['Profit'].append(int(profitmargin1))

df['Item Name'].append(str(name2))
df['Cost Price'].append(int(CostPrice2))
df['Amount Sold'].append(int(NumberSales2))
df['Profit'].append(int(profitmargin2))


df.head()
print(df)

This is what I need to do... Ask (from the user) for TWO more DIFFERENT items to be added to the “data structure(s)” with the item name, cost price, number of sales and profit margin and then regenerate the data table with the new items displayed. Finally, a summary of the total sales for the month for each item and the final total sales and final total profit. The items and their details will be displayed in alphabetical order. The program will then save the updated items data to the original file.The program will then save the Month Summary Data to new text file called MaySales202_yourInitials.txt.

Following the solution I mentioned above in the comments you could do this. I chose this df:

import pandas as pd
df = pd.read_csv("test.csv",
               sep =';')
list(df)

which is:

Item_Name  Cost_Price  Amount_sold  Profit
0      coke           3            5       6
1     bread           5           10      29

You do not need to repeat the code for multiple inputs. You simply need to state when to stop inputing:

while True:
    add = input('Want to add data (Y/N)?: ')
    if (add == 'Y')|(add == 'y'):

        Item_Name = input('Item_Name: ')
        Cost_Price = input('Cost_Price: ')
        Amount_sold = input('Amount_sold: ')
        Profit = input('Profit: ')
        

        d = {'Item_Name':str(Item_Name),'Cost_Price':int(Cost_Price),'Amount_sold':int(Amount_sold),'Profit':int(Profit)}
        print(d)
        df = df.append(d,ignore_index=True)

    else:
        break

print (df)

Inputing some new data returns

Want to add data (Y/N)?: y
Item_Name: fanta
Cost_Price: 4
Amount_sold: 5
Profit: 56
{'Item_Name': 'fanta', 'Cost_Price': 4, 'Amount_sold': 5, 'Profit': 56}
Want to add data (Y/N)?: n
  Item_Name  Cost_Price  Amount_sold  Profit
0      coke           3            5       6
1     bread           5           10      29
2     fanta           4            5      56

Note that if you do not want to lose the inputs, you need to save them:

df.to_csv("test.csv",sep =';')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM