Hướng dẫn importing multiple datasets in python - nhập nhiều bộ dữ liệu trong 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)

Đây là một cách tiếp cận khác, bây giờ giả sử rằng có nhiều tệp và chúng tôi không biết tên và số sau đó, sử dụng mã dưới đây

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
# 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])
9

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

Lưu bài viết

  • Đọc
  • Bàn 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

    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

    Output:

    Hướng dẫn importing multiple datasets in python - nhập nhiều bộ dữ liệu trong python

    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
    
    2
    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
    
    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
    
    4
    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
    
    5
    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

    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

    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
    
    2
    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
    
    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
    
    4
    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
    
    5
    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

    Ở đâ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)
    
    6
    # 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])
    
    7

    Đó 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]:

    Hướng dẫn importing multiple datasets in python - nhập nhiều bộ dữ liệu trong python

    dataframes_list[1]:

    Hướng dẫn importing multiple datasets in python - nhập nhiều bộ dữ liệu trong python

    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
    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
    
    3
    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
    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
    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)
    
    4
    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

    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)
    
    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
    
    3
    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)
    
    8

    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

    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
    
    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
    
    2
    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
    
    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
    
    4
    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
    
    5
    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

    Ở đâ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)
    
    6
    # 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])
    
    7

    Đó 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)
    
    6.read_4

    Output:

    Hướng dẫn importing multiple datasets in python - nhập nhiều bộ dữ liệu trong python
    Hướng dẫn importing multiple datasets in python - nhập nhiều bộ dữ liệu trong python