如何使用包含字符串的行作为数据框列的标题,例如:
Excel 数据:
领域 | 价值观 |
---|---|
F1 | V1 |
F2 | V2 |
F3 | V3 |
F4 | V4 |
以 F2 作为寻求的值,我的数据框应如下所示:
F2 | V2 |
---|---|
F3 | V3 |
F4 | V4 |
我怎样才能实现这个目标,我已经用 xlwings 尝试了很多脚本?
sheet_name='Sheet1'
df = pd.read_excel(file, sheet_name=sheet_name.strip())
search_value='F2'
row_index=df[df.iloc[:,0]==search_value].index[0]
app = xw.App()
wb = app.books.open(file)
# Specify the starting row containing the value
starting_row = row_index
data_range = wb.sheets['Sheet1'].range(f'A{starting_row}').options(pd.DataFrame, header=True, index=False, expand='table').value
wb.close()
app.quit()
print('############### DATAFRAME #################')
print(data_range)
我用过starting_row = row_index +3,没问题。
sheet_name='Sheet1'
df = pd.read_excel(file, sheet_name=sheet_name.strip())
search_value='F2'
row_index=df[df.iloc[:,0]==search_value].index[0]
app = xw.App()
wb = app.books.open(file)
# Specify the starting row containing the value
starting_row = row_index + 3
data_range = wb.sheets['Sheet1'].range(f'A{starting_row}').options(pd.DataFrame, header=True, index=False, expand='table').value
wb.close()
app.quit()
print('############### DATAFRAME #################')
print(data_range)