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.
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.all_files = glob.glob[os.path.join[path, "*.csv"]] df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
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
2all_files = glob.glob[os.path.join[path, "*.csv"]] df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
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
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àiall_files = glob.glob[os.path.join[path, "*.csv"]] df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
- 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
6 để tạo cột mới, với sự quy kết thành nhận xét từ C8H10N4O2all_files = glob.glob[os.path.join[path, "*.csv"]] df = pd.concat[[pd.read_csv[f] for f in all_files], ignore_index=True]
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ả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
1Output:
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
2from 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
4from 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
5from 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
6Python3
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
1from 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
2from 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
4from 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
5from 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]:
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
9from 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]
1dfs = 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]
2dfs = 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]
3dfs = 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]
4dfs = 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]
5Python3
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]
6from 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]
8Trong 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
1from 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
1from 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
2from 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
4from 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
5from 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_
4Output: