Hướng dẫn pandas read excel from s3 - gấu trúc đọc excel từ s3

Tôi có một thiết lập thông báo SNS kích hoạt hàm Lambda khi tệp .xlsx được tải lên thùng S3.

Hàm Lambda đọc tệp .xlsx vào Pandas DataFrame.

import os 
import pandas as pd
import json
import xlrd
import boto3

def main[event, context]:
    message = event['Records'][0]['Sns']['Message']
    parsed_message = json.loads[message]
    src_bucket = parsed_message['Records'][0]['s3']['bucket']['name']
    filepath = parsed_message['Records'][0]['s3']['object']['key']

    s3 = boto3.resource['s3']
    s3_client = boto3.client['s3']

    obj = s3_client.get_object[Bucket=src_bucket, Key=filepath]
    print[obj['Body']]

    df = pd.read_excel[obj, header=2]
    print[df.head[2]]

Tôi gặp lỗi như dưới đây:

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 

Làm thế nào tôi có thể giải quyết điều này?

import io
import pandas as pd

s3 = boto3.client['s3']
obj = s3.get_object[Bucket = 'bucket_name', Key = 'key_name']
df = pd.read_excel[io.BytesIO[obj['Body'].read[]]]


Gợi ý: 2

Hàm Lambda đọc tệp .xlsx anycodings_lambda vào gấu trúc dữ liệu. Kích hoạt hàm Lambda khi tệp .xlsx anycodings_lambda được tải lên thùng S3., Tài liệu ở đây nếu bạn cần thay đổi anycodings_pandas params.

Hàm Lambda đọc tệp .xlsx anycodings_lambda vào gấu trúc DataFrame.anycodings_lambda into Pandas DataFrame.

import os
import pandas as pd
import json
import xlrd
import boto3

def main[event, context]:
   message = event['Records'][0]['Sns']['Message']
parsed_message = json.loads[message]
src_bucket = parsed_message['Records'][0]['s3']['bucket']['name']
filepath = parsed_message['Records'][0]['s3']['object']['key']

s3 = boto3.resource['s3']
s3_client = boto3.client['s3']

obj = s3_client.get_object[Bucket = src_bucket, Key = filepath]
print[obj['Body']]

df = pd.read_excel[obj, header = 2]
print[df.head[2]]

Tôi gặp lỗi như dưới đây:

Invalid file path or buffer object type: : ValueError
   Traceback [most recent call last]:
   File "/var/task/handler.py", line 26, in main
   df = pd.read_excel[obj, header=2]
   File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
   return func[*args, **kwargs]
   File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
   return func[*args, **kwargs]
   File "/var/task/pandas/io/excel.py", line 307, in read_excel
   io = ExcelFile[io, engine=engine]
   File "/var/task/pandas/io/excel.py", line 376, in __init__
   io, _, _, _ = get_filepath_or_buffer[self._io]
   File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
   raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
   ValueError: Invalid file path or buffer object type: 

Nó là hoàn toàn bình thường! obj là một từ điển anycodings_pandas, bạn đã thử chưa?anycodings_pandas dictionnary, have u tried ?

df = pd.read_excel[obj['body'], header = 2]

Nếu obj là một từ điển, bạn có thể thử

df = pd.DataFrame.from_dict[obj]


Gợi ý: 3

Hỗ trợ XLS, XLSX, XLSM, XLSB, ODF, ODF và Tệp ODT được đọc từ hệ thống tập tin hoặc URL cục bộ. Hỗ trợ một tùy chọn để đọc một trang tính hoặc một danh sách các bảng., Đọc một tệp excel vào một gấu trúc DataFrame., DataFrame từ tệp được truyền trong tệp Excel. Xem các ghi chú trong đối số sheet_name để biết thêm thông tin về khi nào một lệnh của DataFrames được trả về.

>>> pd.read_excel['tmp.xlsx', index_col = 0]
Name Value
0 string1 1
1 string2 2
2 #Comment 3

>>> pd.read_excel[open['tmp.xlsx', 'rb'],
   ...sheet_name = 'Sheet3']
Unnamed: 0 Name Value
0 0 string1 1
1 1 string2 2
2 2 #Comment 3

>>> pd.read_excel['tmp.xlsx', index_col = None, header = None]
0 1 2
0 NaN Name Value
1 0.0 string1 1
2 1.0 string2 2
3 2.0 #Comment 3

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
0

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
1

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
2


Gợi ý: 4

Dữ liệu Wrangler là gì?

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
3


Gợi ý: 5

Đọc tệp excel từ S3 vào gấu trúc DataFrame, cách đọc tài liệu tham khảo cột tệp Excel [a..b] vào pandas dataFrame - python, làm cách nào để tạo một tệp dữ liệu gấu trúc từ tệp excel đọc từ yêu cầu http? Nằm bên ngoài thư mục chứa mô -đun vào Pandas DataFrame

Nó là hoàn toàn bình thường! obj là một từ điển, bạn đã thử chưa?

df = pd.read_excel[obj['body'], header = 2]

Nếu

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
9 là từ điển, bạn có thể thử

df = pd.DataFrame.from_dict[obj]


Gợi ý: 6

Đọc tệp Excel từ URL, S3 và từ tệp tệp cục bộ hỗ trợ một số tiện ích mở rộng., Có thể tải các tệp Excel được lưu trữ trong hệ thống tập tin cục bộ hoặc từ URL. & nbsp; xlsb, & nbsp; odf, & nbsp; ods & nbsp; và & nbsp; odt & nbsp;, sau đây là một số tính năng được hỗ trợ bởi read_excel [] với param tùy chọn.

Tôi sẽ đề cập đến cách sử dụng một số thông số tùy chọn này với các ví dụ, trước tiên hãy để Lừa xem cách đọc một tờ Excel và tạo DataFrame mà không cần bất kỳ thông số nào.

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
6

Theo mặc định, nó coi hàng đầu tiên từ Excel là tiêu đề và sử dụng nó làm tên cột DataFrame. Trong trường hợp bạn muốn xem xét hàng đầu tiên từ Excel làm bản ghi dữ liệu, sử dụng param

import io
import pandas as pd

s3 = boto3.client['s3']
obj = s3.get_object[Bucket = 'bucket_name', Key = 'key_name']
df = pd.read_excel[io.BytesIO[obj['Body'].read[]]]
0 và sử dụng tên param để chỉ định tên cột. Không chỉ định tên kết quả trong tên cột có số số.

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
7

Theo mặc định, nó được đặt thành

import io
import pandas as pd

s3 = boto3.client['s3']
obj = s3.get_object[Bucket = 'bucket_name', Key = 'key_name']
df = pd.read_excel[io.BytesIO[obj['Body'].read[]]]
1 có nghĩa là cột không được đặt làm chỉ mục.

Invalid file path or buffer object type: : ValueError
Traceback [most recent call last]:
File "/var/task/handler.py", line 26, in main
df = pd.read_excel[obj, header=2]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/util/_decorators.py", line 178, in wrapper
return func[*args, **kwargs]
File "/var/task/pandas/io/excel.py", line 307, in read_excel
io = ExcelFile[io, engine=engine]
File "/var/task/pandas/io/excel.py", line 376, in __init__
io, _, _, _ = get_filepath_or_buffer[self._io]
File "/var/task/pandas/io/common.py", line 218, in get_filepath_or_buffer
raise ValueError[msg.format[_type=type[filepath_or_buffer]]]
ValueError: Invalid file path or buffer object type: 
8


Làm thế nào để bạn đọc Excel từ S3 bằng gấu trúc?

Boto Boto3 đọc tệp Excel từ S3 vào câu trả lời mã của Pandas..
Nhập IO ..
Nhập Gandas dưới dạng PD ..
S3 = BOTO3. máy khách ['S3'].
obj = s3. get_Object [xô = 'bucket_name', key = 'key_name'].
df = pd. read_excel [io. bytesio [obj ['body']. read []]].

Pandas có thể đọc XLSX không?

Đọc một tập tin Excel vào một bản dữ liệu gấu trúc.Hỗ trợ XLS, XLSX, XLSM, XLSB, ODF, ODF và Tệp ODT được đọc từ hệ thống tập tin hoặc URL cục bộ.Hỗ trợ một tùy chọn để đọc một tờ hoặc một danh sách các tờ.Supports xls , xlsx , xlsm , xlsb , odf , ods and odt file extensions read from a local filesystem or URL. Supports an option to read a single sheet or a list of sheets.

Làm thế nào để gấu trúc đọc các tệp excel trong Jupyter?

Dòng kết quả dưới Jupyter như sau.Bộ dữ liệu trông rất giống với tệp CSV trước được đọc. Nhận Jupyter cho khoa học dữ liệu ngay bây giờ với nền tảng học tập O'Reilly ...
Chọn trang tính trong tệp Excel để đọc ..
Bỏ qua hàng ..
Chỉ định xử lý các giá trị NA ..

Làm cách nào để tải tệp .xlsx vào gấu trúc?

Chức năng pandas.read_excel [] được sử dụng để đọc bảng Excel với phần mở rộng XLSX vào gấu trúc DataFrame.Bằng cách đọc một tờ duy nhất, nó trả về một đối tượng DataFrame của gấu trúc, nhưng đọc hai tờ, nó trả về một dict của DataFrame.Có thể tải các tệp Excel được lưu trữ trong hệ thống tập tin cục bộ hoặc từ URL. read_excel[] function is used to read excel sheet with extension xlsx into pandas DataFrame. By reading a single sheet it returns a pandas DataFrame object, but reading two sheets it returns a Dict of DataFrame. Can load excel files stored in a local filesystem or from an URL.

Bài Viết Liên Quan

Chủ Đề