У меня есть фрейм данных, и я хочу пройти через столбец города. Отфильтровать городские данные и добавить в основной файл, который является шаблоном, и сохранить файл с именем города. Главный файл содержит тот же столбец с номером, что и шаблон df.wth форматирование. И имеет 3 листа .
'State': ['Colorado', 'Colorado', 'Colorado', 'Colorado'],
'County': ['Denver', 'El Paso', 'Larimar', 'Larimar'],
'City': ['Denver', 'Colorado Springs', 'Fort Collins', 'Loveland']
})
for city_name in df['City'].unique():
df_city=df[df['City']==city_name]
for val in df_city['Country'].unique():
accountname=val
template=pd.DataFrame(columns=['State','Country','City','Pincode','Corporater','Population'])
template['State']=df_city['State']
template['Country']=df_city['Country']
template['City']=df_city['City']
template['Pincode']=''
template['Corporater']='Mr.'
template['Population']=''
template=template.reset_index()
#load Master file with some fix formatting and columns want to ad data from template into Master file and save file
workbook=load_workbook('Master.xlsm',keep_vba=True)
sheet=workbook['Ind']
title1=f"{vote_startdate.strftime('%d%d%y')}"
last_row=last_row+1
for index,row in template.iterrows():
for col,value in enumerate(row,start=2):
sheet.cell(rows=last_row+index,column=col,value=value)
sheet.cell(row=2,column=2,value=accountname)
for row in range(sheet.max_row,0,-1):
if sheet.cell(row=row,column=6).value is not None:
last_row=row
break
last_row=last_row+1
if last_row
Подробнее здесь: https://stackoverflow.com/questions/792 ... ame-format
Я хочу перебрать столбец и добавить отфильтрованный df в шаблон с тем же форматом, удалить лишние строки.в пандах ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение