|
| 1 | +--- |
| 2 | +title: brief_alternate Assignment |
| 3 | +date: '2024.01.01 0:00' |
| 4 | +tags: |
| 5 | + - - Python |
| 6 | + - - pandas |
| 7 | +abbrlink: dbbd7d58 |
| 8 | +--- |
| 9 | +# topic |
| 10 | + |
| 11 | +[brief_alternate.pdf](..%2Fassets%2Fdocuments%2Fbrief_alternate.pdf) |
| 12 | +[superstore_transaction.csv](..%2Fassets%2Fdocuments%2Fsuperstore_transaction.csv) |
| 13 | + |
| 14 | +# Thought: |
| 15 | + |
| 16 | +Self -studypandasData processing,Before`import csv`Make much more useful。 |
| 17 | + |
| 18 | +What the knowledge point needs to be remembered is: |
| 19 | + |
| 20 | +1. df["Name"] You can directly determine the first line as the name listed,并且返回一个只包含Nameof列表。 |
| 21 | +2. idxmax()Return to the maximum valueindex,max()Maximum meal |
| 22 | +3. `.loc `The function is pandas An important function in it,Used to select and locate data in the data box。It allows you to choose some lines and columns,To make it DataFrame or Series Back to form。 |
| 23 | + By using the line label and column label as the index,You can perform the following operations in the data box: |
| 24 | + .Choose a single line of data |
| 25 | + .Choose multi -line data |
| 26 | + .Select single -column data |
| 27 | + .Select multiple columns of data |
| 28 | + grammar: `df.loc[row_indexer, column_indexer]` |
| 29 | + in,row_indexer Is the label to choose a line,column_indexer Is the label of the column to be selected。 |
| 30 | +4. unique()Return without duplicationvalueofnumbercount。 |
| 31 | +code show as below: |
| 32 | + |
| 33 | +[superstore](..%2Fassets%2Fdocuments%2Fsuperstore) |
| 34 | +```python s_p |
| 35 | +# Import pandas library as pd |
| 36 | +import pandas as pd |
| 37 | + |
| 38 | +# Read CSV file named 'superstore_transaction.csv' and store it in a dataframe named 'df' |
| 39 | +df = pd.read_csv("superstore_transaction.csv") |
| 40 | + |
| 41 | +# Remove "$" and "," from the values in the 'Profit' column and convert it to integer |
| 42 | +df["Profit"] = df["Profit"].str.replace('$', "").str.replace(",", "").astype(int) |
| 43 | + |
| 44 | +# Remove "$" and "," from the values in the 'Sales' column and convert it to integer |
| 45 | +df["Sales"] = df["Sales"].str.replace('$', "").str.replace(",", "").astype(int) |
| 46 | + |
| 47 | +# Get the index of the row with the maximum value in the 'Profit' column and store it in 'col_max_profit' |
| 48 | +col_max_profit = df["Profit"].idxmax() |
| 49 | +# Get the index of the row with the maximum value in the 'Sales' column and store it in 'col_max_sales' |
| 50 | +col_max_sales = df["Sales"].idxmax() |
| 51 | + |
| 52 | +# Store the details of the transaction with highest sales |
| 53 | +highest_sales_info = [ |
| 54 | + "=========================\n" |
| 55 | + "HIGHEST SALES TRANSACTION\n" |
| 56 | + "=========================\n", |
| 57 | + "Category: {}\n".format(df.loc[col_max_sales, "Category"]), |
| 58 | + "Customer Name: {}\n".format(df.loc[col_max_sales, "Customer Name"]), |
| 59 | + "Product Name: {}\n".format(df.loc[col_max_sales, "Product Name"]), |
| 60 | + "Segment: {}\n".format(df.loc[col_max_sales, "Segment"]), |
| 61 | + "Sub-Category: {}\n".format(df.loc[col_max_sales, "Sub-Category"]), |
| 62 | + "Profit: {}\n".format(df["Sales"].max()), |
| 63 | +] |
| 64 | + |
| 65 | +# Store the details of the transaction with the highest profit |
| 66 | +highest_profit_info = [ |
| 67 | + "==========================\n" |
| 68 | + "HIGHEST PROFIT TRANSACTION\n" |
| 69 | + "==========================\n", |
| 70 | + "Category: {}\n".format(df.loc[col_max_profit, "Category"]), |
| 71 | + "Customer Name: {}\n".format(df.loc[col_max_profit, "Customer Name"]), |
| 72 | + "Product Name: {}\n".format(df.loc[col_max_profit, "Product Name"]), |
| 73 | + "Segment: {}\n".format(df.loc[col_max_profit, "Segment"]), |
| 74 | + "Sub-Category: {}\n".format(df.loc[col_max_profit, "Sub-Category"]), |
| 75 | + "Profit: {}\n".format(df["Profit"].max()), |
| 76 | +] |
| 77 | + |
| 78 | +# Open a file named 'summary_report.txt' in 'append' mode and store it in 'file' |
| 79 | +with open("summary_report.txt", "a") as file: |
| 80 | + # Write the 'highest_sales_info' details to the file |
| 81 | + file.write(''.join(highest_sales_info)) |
| 82 | + file.write(''.join(highest_profit_info)) |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +```python api |
| 87 | +import requests |
| 88 | + |
| 89 | +url = "" |
| 90 | +payload = {} |
| 91 | +headers = { |
| 92 | + "apikey": "" |
| 93 | +} |
| 94 | +r = requests.request("GET", url, headers=headers, data=payload) # responseAPI |
| 95 | +# 查看response结果 |
| 96 | +print("Status code:", r.status_code) |
| 97 | +# Back content isjsonFormat file |
| 98 | +# WillAPIresponse存储在一个变量中 |
| 99 | +# json()Function only decodesjsonFormat return |
| 100 | +response_dict = r.json() |
| 101 | +# print(response_dict) |
| 102 | +# process result 获得response字典 |
| 103 | +# 探索有关仓库of信息 response_dict字典of嵌套 |
| 104 | +# print(response_dict['info']['rate']) |
| 105 | + |
| 106 | +f = open("summary_report.txt", "w") |
| 107 | +head = [ |
| 108 | + "=================================================\n" |
| 109 | + "SINGAPORE TO US DOLLAR EXCHANGE RATE IN REAL TIME\n" |
| 110 | + "=================================================\n" |
| 111 | +] |
| 112 | +f.writelines(head) |
| 113 | +f.writelines([str(response_dict['info']['rate'])+"\n"]) |
| 114 | +f.close() |
| 115 | +print("over") |
| 116 | + |
| 117 | +``` |
| 118 | +```python customers |
| 119 | +import pandas as pd |
| 120 | + |
| 121 | +df = pd.read_csv("superstore_transaction.csv") |
| 122 | +highest_sales_info = [ |
| 123 | + "====================\n" |
| 124 | + "SUPERSTORE CUSTOMERS\n" |
| 125 | + "====================\n", |
| 126 | + "TOTAL: {}\n".format(df["Customer Name"].nunique(), "Category"), |
| 127 | +] |
| 128 | +with open("summary_report.txt", "a") as file: |
| 129 | + file.writelines(highest_sales_info) |
| 130 | + |
| 131 | +``` |
0 commit comments