Hướng dẫn read multiple csv files python - đọc nhiều tệp csv python

Xem Pandas: Công cụ IO cho tất cả các phương thức .read_ có sẵn.

Hãy thử mã sau nếu tất cả các tệp CSV có cùng một cột.

Tôi đã thêm

all_files = glob.glob[os.path.join[path, "*.csv"]]

df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
0, để sau khi đọc hàng đầu tiên của tệp CSV, nó có thể được gán dưới dạng tên cột.

import pandas as pd
import glob
import os

path = r'C:\DRO\DCL_rawdata_files' # use your path
all_files = glob.glob[os.path.join[path , "/*.csv"]]

li = []

for filename in all_files:
    df = pd.read_csv[filename, index_col=None, header=0]
    li.append[df]

frame = pd.concat[li, axis=0, ignore_index=True]

Hoặc, với sự quy kết cho một nhận xét từ SID.

all_files = glob.glob[os.path.join[path, "*.csv"]]

df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
  • Thường cần phải xác định từng mẫu dữ liệu, có thể được thực hiện bằng cách thêm một cột mới vào DataFrame.
  • all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    1 từ thư viện tiêu chuẩn sẽ được sử dụng cho ví dụ này. Nó coi các đường dẫn là đối tượng bằng các phương pháp, thay vì các chuỗi được cắt lát.

Nhập khẩu và thiết lập

from pathlib import Path
import pandas as pd
import numpy as np

path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path

# Get the files from the path provided in the OP
files = Path[path].glob['*.csv']  # .rglob to get subdirectories

Lựa chọn 1:

  • Thêm một cột mới với tên tệp
dfs = list[]
for f in files:
    data = pd.read_csv[f]
    # .stem is method for pathlib objects to get the filename w/o the extension
    data['file'] = f.stem
    dfs.append[data]

df = pd.concat[dfs, ignore_index=True]

Lựa chọn 2:

  • Thêm một cột mới với tên chung bằng cách sử dụng
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    2
dfs = list[]
for i, f in enumerate[files]:
    data = pd.read_csv[f]
    data['file'] = f'File {i}'
    dfs.append[data]

df = pd.concat[dfs, ignore_index=True]

Tùy chọn 3:

  • Tạo DataFrames với khả năng hiểu danh sách, sau đó sử dụng
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    3 để thêm một cột mới.
    • all_files = glob.glob[os.path.join[path, "*.csv"]]
      
      df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
      
      4 Tạo một danh sách các chuỗi để đặt tên cho mỗi DataFrame.
    • all_files = glob.glob[os.path.join[path, "*.csv"]]
      
      df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
      
      5 tạo ra một danh sách độ dài
  • Thuộc tính cho tùy chọn này đi đến câu trả lời âm mưu này.
# Read the files into dataframes
dfs = [pd.read_csv[f] for f in files]

# Combine the list of dataframes
df = pd.concat[dfs, ignore_index=True]

# Add a new column
df['Source'] = np.repeat[[f'S{i}' for i in range[len[dfs]]], [len[df] for df in dfs]]

Tùy chọn 4:

  • Một lớp lót sử dụng
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    6 để tạo cột mới, với sự quy kết thành nhận xét từ C8H10N4O2
df = pd.concat[[pd.read_csv[f].assign[filename=f.stem] for f in files], ignore_index=True]

hoặc

df = pd.concat[[pd.read_csv[f].assign[Source=f'S{i}'] for i, f in enumerate[files]], ignore_index=True]

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    df = pd.read_csv["file path"]

    Bàn luận

    Python3

    Trong bài viết này, chúng ta sẽ thấy cách đọc nhiều tệp CSV vào các khung dữ liệu riêng biệt. Để chỉ đọc một khung dữ liệu, chúng ta có thể sử dụng hàm pd.Read_csv [] của gấu trúc. Nó lấy một đường dẫn làm đầu vào và trả về khung dữ liệu như & nbsp;

    Hãy để một cái nhìn về cách nó hoạt động

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    7
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    8

    Output:

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    9
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    0
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    1
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    2223

    Python3

    Trong bài viết này, chúng ta sẽ thấy cách đọc nhiều tệp CSV vào các khung dữ liệu riêng biệt. Để chỉ đọc một khung dữ liệu, chúng ta có thể sử dụng hàm pd.Read_csv [] của gấu trúc. Nó lấy một đường dẫn làm đầu vào và trả về khung dữ liệu như & nbsp;

    Hãy để một cái nhìn về cách nó hoạt động

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    7
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    8

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    9
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    0
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    1
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    2223

    Ở đây, tội phạm.csv là tệp trong thư mục hiện tại. CSV là thư mục chứa tệp tội phạm và trình đọc csv.ipynb là tệp chứa mã trên.

    dfs = list[]
    for i, f in enumerate[files]:
        data = pd.read_csv[f]
        data['file'] = f'File {i}'
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    3
    # Read the files into dataframes
    dfs = [pd.read_csv[f] for f in files]
    
    # Combine the list of dataframes
    df = pd.concat[dfs, ignore_index=True]
    
    # Add a new column
    df['Source'] = np.repeat[[f'S{i}' for i in range[len[dfs]]], [len[df] for df in dfs]]
    
    4

    Đó là khung dữ liệu được đọc từ hàm trên. Một tệp nữa có mặt trong thư mục có tên - username.csv. Để đọc cả hai và lưu trữ chúng trong các khung dữ liệu khác nhau, hãy sử dụng mã dưới đây

    dataframes_list[0]:

    dataframes_list[1]:

    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    6
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    0
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    8
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    9____________
    dfs = list[]
    for f in files:
        data = pd.read_csv[f]
        # .stem is method for pathlib objects to get the filename w/o the extension
        data['file'] = f.stem
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    1
    dfs = list[]
    for f in files:
        data = pd.read_csv[f]
        # .stem is method for pathlib objects to get the filename w/o the extension
        data['file'] = f.stem
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    2

    Python3

    dfs = list[]
    for f in files:
        data = pd.read_csv[f]
        # .stem is method for pathlib objects to get the filename w/o the extension
        data['file'] = f.stem
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    3
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    0
    dfs = list[]
    for f in files:
        data = pd.read_csv[f]
        # .stem is method for pathlib objects to get the filename w/o the extension
        data['file'] = f.stem
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    5

    Trong bài viết này, chúng ta sẽ thấy cách đọc nhiều tệp CSV vào các khung dữ liệu riêng biệt. Để chỉ đọc một khung dữ liệu, chúng ta có thể sử dụng hàm pd.Read_csv [] của gấu trúc. Nó lấy một đường dẫn làm đầu vào và trả về khung dữ liệu như & nbsp;

    Hãy để một cái nhìn về cách nó hoạt động

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    7
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    8

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    7
    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    8

    all_files = glob.glob[os.path.join[path, "*.csv"]]
    
    df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
    
    9
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    0
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    1
    from pathlib import Path
    import pandas as pd
    import numpy as np
    
    path = r'C:\DRO\DCL_rawdata_files'  # or unix / linux / mac path
    
    # Get the files from the path provided in the OP
    files = Path[path].glob['*.csv']  # .rglob to get subdirectories
    
    2223

    Ở đây, tội phạm.csv là tệp trong thư mục hiện tại. CSV là thư mục chứa tệp tội phạm và trình đọc csv.ipynb là tệp chứa mã trên.

    dfs = list[]
    for i, f in enumerate[files]:
        data = pd.read_csv[f]
        data['file'] = f'File {i}'
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    3
    # Read the files into dataframes
    dfs = [pd.read_csv[f] for f in files]
    
    # Combine the list of dataframes
    df = pd.concat[dfs, ignore_index=True]
    
    # Add a new column
    df['Source'] = np.repeat[[f'S{i}' for i in range[len[dfs]]], [len[df] for df in dfs]]
    
    4

    Đó là khung dữ liệu được đọc từ hàm trên. Một tệp nữa có mặt trong thư mục có tên - username.csv. Để đọc cả hai và lưu trữ chúng trong các khung dữ liệu khác nhau, hãy sử dụng mã dưới đây

    dfs = list[]
    for i, f in enumerate[files]:
        data = pd.read_csv[f]
        data['file'] = f'File {i}'
        dfs.append[data]
    
    df = pd.concat[dfs, ignore_index=True]
    
    3.read_1

    Output:


    Làm cách nào để xem nhiều tệp CSV trong Python?

    Giải thích mã Ở đây, mô -đun GLOB giúp trích xuất thư mục tệp [tên tệp + tên tệp với phần mở rộng], dòng 10 Ném13: Chúng tôi tạo một loại dữ liệu đối tượng loại danh sách để giữ mọi CSV làm khung dữ liệu ở mỗi chỉ mục của danh sách đó. Dòng 15: Chúng tôi gọi PD. Phương thức Concat [] để hợp nhất từng DataFrame trong danh sách theo các cột, nghĩa là Axis = 1.the glob module helps extract file directory [path + file name with extension], Lines 10–13: We create a list type object dataFrames to keep every csv as a DataFrame at each index of that list. Line 15: We call pd. concat[] method to merge each DataFrame in the list by columns, that is, axis=1 .

    Làm cách nào để đọc nhiều tệp CSV?

    Sử dụng gói READR Bạn có thể coi đây là tùy chọn thứ ba để tải nhiều tệp CSV vào R DataFrame, phương thức này sử dụng gói readr chức năng read_csv [].READR là thư viện của bên thứ ba do đó, để sử dụng thư viện Readr, trước tiên bạn cần cài đặt nó bằng cách sử dụng Cài đặt. You can consider this as a third option to load multiple CSV files into R DataFrame, This method uses the read_csv[] function readr package. readr is a third-party library hence, in order to use readr library, you need to first install it by using install.

    Làm thế nào đọc nhiều tệp trong gấu trúc?

    Nhập khẩu Quả cầu ..
    Nhập hệ điều hành ..
    Nhập Gandas dưới dạng PD ..
    all_files = glob.Quả cầu ["Động vật/*. CSV"].
    df = pd.Concat [[pd. read_csv [f] cho f trong all_files]].
    print[df].

    Làm cách nào để hợp nhất nhiều tệp CSV thành gấu trúc?

    Để hợp nhất tất cả các tệp CSV, sử dụng mô -đun GLOB.Hệ điều hành.đường dẫn.Phương thức nối [] được sử dụng bên trong Concat [] để hợp nhất các tệp CSV lại với nhau.use the GLOB module. The os. path. join[] method is used inside the concat[] to merge the CSV files together.

    Bài Viết Liên Quan

    Chủ Đề