我已经在谷歌实验室工作了两个小时,试图让我的代码能够直观地表示数据。问题是,当我读文件,他们不是csv的,我不太确定如何将它们转换回csv。然而,没有什么我尝试工作,实际上得到的数据是直观地表示。另一个主要问题是python认为所有的对象都是object,所以它不允许我将它们转换为float或int。请帮助。
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
headers = ['Order ID', 'Product', 'Quantity Ordered', 'Price Each', 'Order Date', 'Purchase Address']
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_January_2019.csv'
January = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_February_2019.csv'
February = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_March_2019.csv'
March = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_April_2019.csv'
April = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_May_2019.csv'
May = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_June_2019.csv'
June = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_July_2019.csv'
July = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_August_2019.csv'
August = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_September_2019.csv'
September = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_October_2019.csv'
October = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_November_2019.csv'
November = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_December_2019.csv'
December = pd.read_csv(url)
January.head()
January.dtypes
January['Price Each'] = January['Price Each'].astype(float)
我想明白了:
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
import csv
headers = ['Order ID', 'Product', 'Quantity Ordered', 'Price Each', 'Order Date', 'Purchase Address']
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_January_2019.csv'
January = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_February_2019.csv'
February = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_March_2019.csv'
March = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_April_2019.csv'
April = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_May_2019.csv'
May = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_June_2019.csv'
June = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_July_2019.csv'
July = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_August_2019.csv'
August = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_September_2019.csv'
September = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_October_2019.csv'
October = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_November_2019.csv'
November = pd.read_csv(url, names=headers)
url = 'https://raw.githubusercontent.com/cshort2112/Sample-data/main/Sales_December_2019.csv'
December = pd.read_csv(url)
January.columns
January = January.to_csv('January.csv')
January
x = []
y = []
with open('January.csv') as csvfile:
plots = csv.reader(csvfile, delimiter = ',')
for row in plots:
x.append(row[1])
y.append(row[2])
plt.bar(x, y, color = 'g', width = 0.72, label = "test")
plt.xlabel('Product')
plt.ylabel('Quantity')
plt.title('Qty by product')
plt.legend()
plt.show()