Cách thêm nhiều mảng trong python

Một số nghiên cứu thú vị nhất về dữ liệu đến từ việc kết hợp các nguồn dữ liệu khác nhau. Các hoạt động này có thể liên quan đến bất kỳ thứ gì, từ việc ghép nối rất đơn giản hai bộ dữ liệu khác nhau, đến các phép nối và hợp nhất kiểu cơ sở dữ liệu phức tạp hơn để xử lý chính xác bất kỳ sự trùng lặp nào giữa các bộ dữ liệu. Các

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
1 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 được xây dựng có lưu ý đến loại hoạt động này và Pandas bao gồm các hàm và phương thức giúp sắp xếp loại dữ liệu này nhanh chóng và đơn giản

Ở đây chúng ta sẽ xem xét phép nối đơn giản của

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
1 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 với hàm
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
5;

Chúng tôi bắt đầu với việc nhập khẩu tiêu chuẩn

Trong 1]

import pandas as pd
import numpy as np

Để thuận tiện, chúng tôi sẽ xác định chức năng này tạo ra một

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 của một hình thức cụ thể sẽ hữu ích dưới đây

Trong 2]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]

Ra[2]

ABC0A0B0C01A1B1C12A2B2C2

Ngoài ra, chúng tôi sẽ tạo một lớp nhanh cho phép chúng tôi hiển thị nhiều

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 cạnh nhau. Đoạn mã này sử dụng phương thức
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
8 đặc biệt mà IPython sử dụng để triển khai hiển thị đối tượng phong phú của nó

Trong 3]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]

Việc sử dụng điều này sẽ trở nên rõ ràng hơn khi chúng ta tiếp tục thảo luận trong phần sau

Hồi tưởng. Ghép các mảng NumPy¶

Nối các đối tượng

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
1 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 rất giống với nối các mảng Numpy, có thể được thực hiện thông qua hàm
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
31 như đã thảo luận trong Khái niệm cơ bản về mảng NumPy. Nhớ lại rằng với nó, bạn có thể kết hợp nội dung của hai hoặc nhiều mảng thành một mảng duy nhất

Trong [4]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
3

Ra[4]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
5

Đối số đầu tiên là một danh sách hoặc bộ mảng để nối. Ngoài ra, cần có một từ khóa

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
32 cho phép bạn chỉ định trục dọc theo đó kết quả sẽ được nối

Trong [5]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
7

Ra[5]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
8

Nối đơn giản với
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]

Pandas có một chức năng,

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
34, có cú pháp tương tự như
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
31 nhưng chứa một số tùy chọn mà chúng ta sẽ thảo luận trong giây lát

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
34 có thể được sử dụng để nối đơn giản các đối tượng
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
1 hoặc
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2, cũng như
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
39 có thể được sử dụng để nối đơn giản các mảng

Trong [6]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
7

Ra[6]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
8

Nó cũng hoạt động để nối các đối tượng có chiều cao hơn, chẳng hạn như

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2

Trong [7]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
0

Ra[7]

df1

AB1A1B12A2B2

df2

AB3A3B34A4B4

pd. concat[[df1, df2]]

AB1A1B12A2B23A3B34A4B4

Theo mặc định, việc nối diễn ra theo hàng trong

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 [i. e. ,
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
52]. Giống như
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
31,
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
5 cho phép xác định một trục dọc theo đó quá trình nối sẽ diễn ra. Xem xét ví dụ sau

Trong [8]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
1

Ra[8]

df3

AB0A0B01A1B1

df4

CD0C0D01C1D1

pd. concat[[df3, df4], axis='col']

ABCD0A0B0C0D01A1B1C1D1

Chúng tôi có thể đã chỉ định tương đương

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
55;

Sao chép chỉ số¶

Một điểm khác biệt quan trọng giữa

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
31 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
5 là phép nối Pandas bảo toàn các chỉ số, ngay cả khi kết quả sẽ có các chỉ số trùng lặp. Hãy xem xét ví dụ đơn giản này

Trong [9]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
2

Ra[9]

x

AB0A0B01A1B1

y

AB0A2B21A3B3

pd. concat[[x, y]]

AB0A0B01A1B10A2B21A3B3

Lưu ý các chỉ số lặp lại trong kết quả. Mặc dù điều này có giá trị trong vòng

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2s, nhưng kết quả thường không mong muốn.
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
34 cho ta vài cách xử lý

Bắt lỗi lặp lại¶

Nếu bạn chỉ muốn xác minh rằng các chỉ số trong kết quả của

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
34 không trùng nhau, bạn có thể chỉ định cờ
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
72. Với giá trị này được đặt thành True, phép nối sẽ đưa ra một ngoại lệ nếu có các chỉ số trùng lặp. Đây là một ví dụ, để rõ ràng, chúng tôi sẽ bắt và in thông báo lỗi

Trong [10]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
3

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
4

Bỏ qua chỉ mục¶

Đôi khi bản thân chỉ mục không quan trọng và bạn chỉ muốn bỏ qua nó. Tùy chọn này có thể được chỉ định bằng cách sử dụng cờ

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
73. Với giá trị này được đặt thành true, phép nối sẽ tạo ra một chỉ mục số nguyên mới cho kết quả là
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
1

Trong [11]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
5

Ra[11]

x

AB0A0B01A1B1

y

AB0A2B21A3B3

pd. concat[[x, y], ignore_index=True]

AB0A0B01A1B12A2B23A3B3

Thêm khóa MultiIndex¶

Một tùy chọn khác là sử dụng tùy chọn

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
75 để chỉ định nhãn cho nguồn dữ liệu;

Trong [12]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
6

Ra[12]

x

AB0A0B01A1B1

y

AB0A2B21A3B3

pd. concat[[x, y], keys=['x', 'y']]

ABx0A0B01A1B1y0A2B21A3B3

Kết quả là một

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 được lập chỉ mục nhiều lần và chúng tôi có thể sử dụng các công cụ được thảo luận trong Lập chỉ mục theo cấp bậc để chuyển đổi dữ liệu này thành biểu diễn mà chúng tôi quan tâm

Nối với phép nối¶

Trong các ví dụ đơn giản mà chúng ta vừa xem xét, chúng ta chủ yếu nối các

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 với các tên cột được chia sẻ. Trong thực tế, dữ liệu từ các nguồn khác nhau có thể có các bộ tên cột khác nhau và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
5 cung cấp một số tùy chọn trong trường hợp này. Xem xét sự kết hợp của hai
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 sau đây, chúng có một số [nhưng không phải tất cả]. ] cột chung

Trong [13]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
7

Ra[13]

df5

ABC1A1B1C12A2B2C2

df6

BCD3B3C3D34B4C4D4

pd. concat[[df5, df6]]

ABCD1A1B1C1NaN2A2B2C2NaN3NaNB3C3D34NaNB4C4D4

Theo mặc định, các mục nhập không có sẵn dữ liệu sẽ chứa các giá trị NA. Để thay đổi điều này, chúng ta có thể chỉ định một trong số các tùy chọn cho các tham số

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
80 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
81 của hàm nối. Theo mặc định, phép nối là sự kết hợp của các cột đầu vào [
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
82], nhưng chúng ta có thể thay đổi điều này thành một giao điểm của các cột bằng cách sử dụng
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
83

Trong [14]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
8

Ra[14]

df5

ABC1A1B1C12A2B2C2

df6

BCD3B3C3D34B4C4D4

pd. concat[[df5, df6], tham gia='bên trong']

BC1B1C12B2C23B3C34B4C4

Một tùy chọn khác là chỉ định trực tiếp chỉ mục của các cột còn lại bằng cách sử dụng đối số ________ 181, lấy danh sách các đối tượng chỉ mục. Ở đây, chúng tôi sẽ chỉ định rằng các cột được trả về phải giống với các cột của đầu vào đầu tiên

Trong [15]

def make_df[cols, ind]:
    """Quickly make a DataFrame"""
    data = {c: [str[c] + str[i] for i in ind]
            for c in cols}
    return pd.DataFrame[data, ind]

# example DataFrame
make_df['ABC', range[3]]
9

Ra[15]

df5

ABC1A1B1C12A2B2C2

df6

BCD3B3C3D34B4C4D4

pd. concat[[df5, df6], join_axes=[df5. cột]]

ABC1A1B1C12A2B2C23NaNB3C34NaNB4C4

Sự kết hợp các tùy chọn của hàm

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
5 cho phép một loạt các hành vi có thể xảy ra khi tham gia hai bộ dữ liệu;

Phương pháp
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
86¶

Bởi vì nối mảng trực tiếp rất phổ biến, các đối tượng

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
1 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 có một phương thức
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
89 có thể thực hiện điều tương tự với ít lần nhấn phím hơn. Ví dụ: thay vì gọi
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
20, bạn chỉ cần gọi
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
21

Trong [16]

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
0

Ra[16]

df1

AB1A1B12A2B2

df2

AB3A3B34A4B4

df1. nối thêm [df2]

AB1A1B12A2B23A3B34A4B4

Hãy nhớ rằng không giống như các phương thức

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
86 và
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
23 trong danh sách Python, phương thức
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
86 trong Pandas không sửa đổi đối tượng ban đầu – thay vào đó, nó tạo một đối tượng mới với dữ liệu được kết hợp. Nó cũng không phải là một phương pháp rất hiệu quả, bởi vì nó liên quan đến việc tạo một bộ đệm dữ liệu và chỉ mục mới. Do đó, nếu bạn dự định thực hiện nhiều thao tác
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
89, tốt hơn hết là xây dựng danh sách các
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
2 và chuyển tất cả chúng cùng một lúc cho hàm
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
27

Trong phần tiếp theo, chúng ta sẽ xem xét một cách tiếp cận mạnh mẽ hơn khác để kết hợp dữ liệu từ nhiều nguồn, hợp nhất/kết hợp kiểu cơ sở dữ liệu được triển khai trong

class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
28. Để biết thêm thông tin về
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
27,
class display[object]:
    """Display HTML representation of multiple objects"""
    template = """

{0}

{1}
"""
def __init__[self, *args]: self.args = args def _repr_html_[self]: return '\n'.join[self.template.format[a, eval[a]._repr_html_[]] for a in self.args] def __repr__[self]: return '\n\n'.join[a + '\n' + repr[eval[a]] for a in self.args]
86 và chức năng liên quan, hãy xem phần "Hợp nhất, Tham gia và Ghép" trong tài liệu Pandas

Chủ Đề