![](/img/trans.png)
[英]when to pass values such as 'none' to function in python pd.read_excel()
[英]pd.read_excel in Python
我當前正在使用read_excel來訪問我的excel文件,並且我已經通過引用該列的第一個單元格對象( excel表格中的列標題 )為工作表中的每一列分配了變量。 現在,這可以正常工作,我僅通過引用分配給我的變量就能夠對每一列中的數據進行排序。 但是,當我將sheetname參數添加到read_excel以便將我的排序代碼應用於具有相同列位置和第一個單元格對象( excel工作表中的列標題 )的excel文件中的其他工作表時,出現錯誤。 我該怎么辦?
import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
#import os
#import collections
from collections import defaultdict
#import pylab as pl
#import xlrd
import warnings; warnings.simplefilter('ignore')
# reading the excel sheet
df = pd.read_excel('file_path.xlsx')
# assigning variables to each column
gender = df['Gender']
name = df['Name']
age = df['Age']
district = df['District']
school = df['School']
private_public = df['Is your school']
settlement_type = df['Type of settlement']
我收到此錯誤消息
文件“ C:/ Users / qanda / OneDrive / Documents / Python Scripts / PEN / pen_data_man.py”,第25行,性別= df ['Gender'] KeyError:'Gender'
當我添加sheetname =無
df = pd.read_excel('file_path.xlsx')
至
df = pd.read_excel('file_path.xlsx', sheetname=None)
如果檢查文檔 :
參數sheetname允許指定要讀取的一個或多個工作表。
工作表名稱的默認值為0,表示要讀取第一張工作表
傳遞字符串以引用工作簿中特定工作表的名稱
傳遞整數以引用工作表的索引。 索引遵循Python約定,從0開始。
傳遞字符串或整數列表,以返回指定工作表的字典。
傳遞“ 無”以返回所有可用工作表的字典 。
因此,如果添加None
參數,則獲取所有DataFrames
dictionary
-對於每個工作表DataFrames
為df
:
dfs = pd.read_excel('multiple_sheets.xlsx', sheetname=None)
print (dfs)
{'sheetname2': t w
0 7 1
1 5 0, 'sheetname1': a b
0 1 4
1 2 8}
df1 = dfs['sheetname1']
print (df1)
a b
0 1 4
1 2 8
df2 = dfs['sheetname2']
print (df2)
t w
0 7 1
1 5 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.