我正在尝试创建一个带有两个没有索引的标头的csv,如下所示:
Inventory,,,,
PartNumber,Quantity,VendorName,FulfillmentCenterName,Delete
2056,00000037,cl Vendor,010039,0
2058,00000040,cl Vendor,010039,0
2065,00000083,cl Vendor,010039,0
2066,00000011,cl Vendor,010039,0
2067,00000000,cl Vendor,010039,0
我的脚本开始通过API下载文件,然后转换它的格式以减少列并再次重命名这些列。
我的问题是我无法添加第一个标题库存。我可以添加第二个。
这是我的代码
import pandas as pd
import numpy as np
import requests
import io
import pandas as pd
import requests
import io
question = input('update data? y/n: ')
if question == 'y':
print ('Actualizando...')
url = 'http://.net/api/offers'
r = requests.get(url, headers={'Authorization': 'aea2-a3e4a32afdc4'})
i = io.StringIO(r.content.decode('utf-8'))
OF51_ALL = pd.read_csv(i,sep=';', engine = 'python')
cols = ['offer-id','quantity','shop-id','shop-name']
OF51_ALL_SELECTED = OF51_ALL[cols]
df = pd.DataFrame(OF51_ALL_SELECTED)
writer = pd.ExcelWriter('C:\\Users\\local\\parentfile.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
print ('update OK')
else: print('->')
datos = pd.read_excel(open('parentfile.xlsx','rb'), sheet_name = 'Sheet1')
datos['quantity'] = datos['quantity'].astype(str)
datos['offer-id'] = datos['offer-id'].astype(str)
datos['lenghts'] = datos['quantity'].apply(len)
datos.loc[(datos['lenghts'] == 1, 'new_col')] = '0000000'
datos.loc[(datos['lenghts'] == 2, 'new_col')] = '000000'
datos.loc[(datos['lenghts'] == 3, 'new_col')] = '00000'
datos.loc[(datos['lenghts'] == 4, 'new_col')] = '0000'
datos.loc[(datos['lenghts'] == 5, 'new_col')] = '000'
datos.loc[(datos['lenghts'] == 6, 'new_col')] = '00'
datos.loc[(datos['lenghts'] == 7, 'new_col')] = '0'
datos.loc[(datos['lenghts'] == 8, 'new_col')] = ''
datos['Quantity'] = datos['new_col']+datos['quantity']
datos.loc[(datos['lenghts'] <=8 , 'VendorName')]='cl Vendor'
datos.loc[(datos['lenghts'] <=8 , 'FulfillmentCenterName')]='010039'
datos.loc[(datos['lenghts'] <=8 , 'Delete')]='0'
datos.columns = ['PartNumber','quantity','shop-id','shop-name','len','z','Quantity','VendorName','FulfillmentCenterName','Delete']
eleccion = input('create: all or number?: ')
if eleccion == 'number':
selecid = int(input('indicate: '))
filtered=(datos.loc[datos['shop-id'] == selecid])
indexdata = filtered.set_index('PartNumber')
print (indexdata['shop-name'])
print ('File was saved')
indexdata.to_csv('C:\\file.csv', index = 'false', encoding = 'utf-8-sig', columns=['Quantity','VendorName','FulfillmentCenterName','Delete'])
elif eleccion == 'all':
indexdata2 = datos.set_index('PartNumber')
indexdata2.to_csv('C:\\file.csv',index = 'false', encoding = 'utf-8-sig', columns=['Quantity','VendorName','FulfillmentCenterName','Delete'])
print ('File was saved')
else: print ('ERROR')
答案 0 :(得分:0)
我和之前的评论一样困惑,但是看起来您正在尝试转换为包含列标题具有层次结构的csv。您可以使用df.columns.droplevel()
消除此问题,然后再写入csv。
cols = pd.MultiIndex.from_tuples([("Inventory", "PartNumber"),
("Inventory", "Quantity")
("Inventory", "VendorName")])
df = pd.DataFrame([[2056,00000037,cl Vendor], [2058,00000040,cl Vendor]], columns=cols)
df
-----------
Inventory
PartNumber Quantity VendorName
2056 00000037 cl Vendor
2058 00000040 cl Vendor
。
df.columns = df.columns.droplevel()
df
----------
PartNumber Quantity VendorName
2056 00000037 cl Vendor
2058 00000040 cl Vendor
df.to_csv('filename.csv')
答案 1 :(得分:0)
使用此代码,我们将创建一个csv文件,然后添加所需的2行标题,然后填充数据。
import csv
data = [("2056", "00000037", "c1 Vendor", "010039", "0"),
("2058", "00000040", "c1 Vendor", "010039", "0"),
("2065", "00000083", "c1 Vendor", "010039", "0"),
("2066", "00000011", "c1 Vendor", "010039", "0"),
("2067", "00000000", "c1 Vendor", "010039", "0"),]
with open('csv_file.csv', 'w', newline="") as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['Inventory', '', '', '', ''])
filewriter.writerow(['PartNumber', 'Quantity', "VendorName", "FulfillmentCenterName", "Delete"])
for elt in data:
filewriter.writerow(elt)