“ MaxU”发布的辅助函数“ append_df_to_excel”对我来说很好用。先前文章的网址如下:
How to write to an existing excel file without overwriting data (using pandas)?
我有一个相关问题。使用“ set_index”更改索引后,如何“保存” DataFrame?
对于启动DataFrame,索引将是0、1、2、3、4等。但是当我使用“ set_index”对Index进行更改时,则期望将DataFrame保存到新索引< / strong>。
让我用一个例子来阐明:
import os
import sys
import pandas as pd
import numpy as np
data = {
'Sr#' : [ '1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59','60','61','62' ]
, 'Sector' : [ 'Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Financials','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Technology','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary','Consumer Discretionary' ]
, 'ETF' : [ 'XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLF','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLK','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY','XLY' ]
, 'Underlying Ticker' : [ 'WFC','JPM','BAC','C','USB','PNC','GS','MS','BK','BLK','SCHW','STT','AXP','COF','BRK.B','AIG','MET','PRU','ACE','TRV','SPG','AMT','CSCO','QCOM','T','VZ','FB','GOOGL','GOOG','V','IBM','MA','ACN','CTSH','PYPL','INTC','TXN','MSFT','ORCL','CRM','AAPL','EMC','HPQ','JCI','F','GM','MCD','SBUX','YUM','AMZN','PCLN','NFLX','DIS','CMCSA','TWX','FOXA','TWC','TGT','HD','LOW','TJX','NKE' ]
, 'Underlying Name' : [ 'Wells Fargo & Company','JPMorgan Chase & Co.','Bank of America Corporation','Citigroup Inc.','U.S. Bancorp','PNC Financial Services Group Inc.','Goldman Sachs Group Inc.','Morgan Stanley','Bank of New York Mellon Corporation','BlackRock Inc.','Charles Schwab Corporation','State Street Corporation','American Express Company','Capital One Financial Corporation','Berkshire Hathaway Inc. Class B','American International Group Inc.','MetLife Inc.','Prudential Financial Inc.','ACE Limited','Travelers Companies Inc.','Simon Property Group Inc.','American Tower Corporation','Cisco Systems Inc.','QUALCOMM Incorporated','AT&T Inc.','Verizon Communications Inc.','Facebook Inc. Class A','Google Inc. Class A','Google Inc. Class C','Visa Inc. Class A','International Business Machines Corporation','MasterCard Incorporated Class A','Accenture Plc','Cognizant Technology Solutions Corporation Class A','PayPal Holdings Inc','Intel Corporation','Texas Instruments Incorporated','Microsoft Corporation','Oracle Corporation','salesforce.com inc.','Apple Inc.','EMC Corporation','Hewlett-Packard Company','Johnson Controls Inc.','Ford Motor Company','General Motors Company','McDonald\'s Corporation','Starbucks Corporation','YUM! Brands Inc.','Amazon.com Inc.','Priceline Group Inc','Netflix Inc.','Walt Disney Company','Comcast Corporation Class A','Time Warner Inc.','Twenty-First Century Fox Inc. Class A','Time Warner Cable Inc.','Target Corporation','Home Depot Inc.','Lowe\'s Companies Inc.','TJX Companies Inc.','NIKE Inc. Class B' ]
, 'Industry' : [ 'Banks','Banks','Banks','Banks','Banks','Banks','Capital Markets','Capital Markets','Capital Markets','Capital Markets','Capital Markets','Capital Markets','Consumer Finance','Consumer Finance','Diversified Financial Services','Insurance','Insurance','Insurance','Insurance','Insurance','Real Estate Investment Trusts (Reits)','Real Estate Investment Trusts (Reits)','Communications Equipment','Communications Equipment','Diversified Telecommunication Services','Diversified Telecommunication Services','Internet Software & Services','Internet Software & Services','Internet Software & Services','It Services','It Services','It Services','It Services','It Services','It Services','Semiconductors & Semiconductor Equipment','Semiconductors & Semiconductor Equipment','Software','Software','Software','Technology Hardware Storage & Peripherals','Technology Hardware Storage & Peripherals','Technology Hardware Storage & Peripherals','Auto Components','Automobiles','Automobiles','Hotels Restaurants & Leisure','Hotels Restaurants & Leisure','Hotels Restaurants & Leisure','Internet & Catalog Retail','Internet & Catalog Retail','Internet & Catalog Retail','Media','Media','Media','Media','Media','Multiline Retail','Specialty Retail','Specialty Retail','Specialty Retail','Textiles Apparel & Luxury Goods' ]
}
df_sectors_original = pd.DataFrame(data)
df_sectors_indexed = df_sectors_original[['Sector','ETF','Underlying Ticker','Underlying Name','Industry']]
df_sectors_indexed.set_index("Industry",drop=True,inplace=True)
output_file = 'C:\my_python\output.xlsx'
append_df_to_excel(output_file, df_sectors_indexed, sheet_name='Indexed', index=False)