我正在尝试将行转换为在嵌套for循环中生成的列。
简而言之就是这样:
值1在行中,属于值1的数据必须作为列
值2在行中,属于值2的数据必须作为列
现在的情况是
所有值都导出为行,之后,值的所有值都导出为行,这将使其无法读取。
问题是要得到价值1,价值2等等…我必须遍历for循环,要获取值1的所有数据,我需要遍历另一个for循环(嵌套循环)。
我收集的所有数据都来自一个网站(抓取)。
我已经包括了imgurl链接到它是如何和应该如何(我的进展到目前为止)。第一个是它是怎样的,第二个是它应该是怎样的。我相信用形象来解释比用我自己的话更容易。
https://imgur.com/a/2LRhQrj
我使用pandas和xlsxwriter来存储到excel。
我设法将所有数据导出到excel中,但似乎无法将每个值的值转换为列。
第一行是时间。这是应该的工作方式。
#Initialize things before loop
df = pd.DataFrame()
### Time based on hour 00:00, 01:00 etc...
df_time = pd.DataFrame(columns=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
for listing in soup.find_all('tr'):
listing.attrs = {}
#assetTime = listing.find_all("td", {"class": "locked"})
assetCell = listing.find_all("td", {"class": "assetCell"})
assetValue = listing.find_all("td", {"class": "assetValue"})
for data in assetCell:
array = [data.get_text()]
df = df.append(pd.DataFrame({
'Fridge name': array,
}))
for value in assetValue:
asset_array = [value.get_text()]
df_time = df_time.append(pd.DataFrame({
'Temperature': asset_array
}))
### End of assetValue loop
### End of assetCell loop
### Now we need to save the data to excel
### Create a Pandas Excel writer using XlsxWriter as the Engine
writer = pd.ExcelWriter(filename+'.xlsx', engine='xlsxwriter')
### Convert dataframes
frames = [df, df_time]
result = pd.concat(frames)
### Convert the dataframe to an XlsxWriter Excel object and skip first row for custom header
result.to_excel(writer, sheet_name='SheetName', startrow=1, header=True)
### Get the xlsxwritert workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets['SheetName']
### Write the column headers with the defined add_format
for col_num, value in enumerate(result.columns.values):
worksheet.write(0, col_num +1, value)
### Close Pandas Excel writer and output the Excel file
writer.save()