Khi học một ngôn ngữ lập trình, bạn có thể bắt gặp một kiểu dữ liệu được gọi là chuỗi. Một chuỗi thường chứa một loạt các ký tự được lồng trong dấu ngoặc kép có thể được biểu diễn dưới dạng văn bản
Trong bài viết này, chúng ta sẽ nói về nối chuỗi trong Python. Đây là quá trình nối/thêm một chuỗi vào một chuỗi khác. Ví dụ: từ nối của "freeCode" và "Camp" là "freeCodeCamp"
Nối chuỗi rất quan trọng khi làm việc với hai hoặc nhiều biến [chuỗi] riêng biệt được kết hợp để tạo thành một chuỗi lớn hơn nhiều
Điều này cũng cho phép bạn có các đơn vị chuỗi riêng biệt ngay cả sau khi kết hợp chúng, chỉ trong trường hợp bạn cần sử dụng một biến chuỗi ở một nơi khác trong mã của mình chứ không phải toàn bộ chuỗi
Cách nối các chuỗi trong Python
Để nối chuỗi trong Python, chúng ta sử dụng toán tử +
để nối các chuỗi với nhau. Đây là một ví dụ
x = "Happy"
y = "Coding"
z = x + y
print[z]
#HappyCoding
Trong đoạn mã trên, chúng tôi đã tạo hai biến [x
và y
] đều chứa các chuỗi – "Happy" và "Coding" – và một biến thứ ba [z
] kết hợp hai biến chúng tôi đã tạo ban đầu
Chúng tôi có thể kết hợp hai biến bằng cách sử dụng toán tử +
. Đầu ra của chúng tôi sau đó là
x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
0. Nếu bạn đảo ngược thứ tự trong quá trình nối bằng cách này. x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
1 thì chúng ta sẽ nhận được x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
2 được in ra bàn điều khiểnCách thêm khoảng trắng giữa các chuỗi được nối
Bạn có thể nhận thấy rằng không có khoảng cách giữa các biến khi được in. Đây là cách chúng ta có thể thêm khoảng trắng giữa các chuỗi được nối
x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
Bạn sẽ nhận thấy rằng có một khoảng cách giữa các dấu ngoặc kép. Nếu bạn bỏ khoảng trắng thì các chuỗi vẫn được nối liền với nhau
Bạn cũng có thể thêm dấu cách vào cuối chuỗi khi chuỗi được tạo và dấu cách sẽ được áp dụng khi in. Đây là cách bạn muốn làm điều đó
x = "Happy "
y = "Coding"
z = x + y
print[z]
#Happy Coding
Cách tạo nhiều bản sao của một chuỗi bằng toán tử x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
3
x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
Khi chúng tôi sử dụng toán tử
x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
3 trên một chuỗi, tùy thuộc vào giá trị được truyền, chúng tôi tạo và nối [nối] các bản sao của chuỗi. Đây là một ví dụ________số 8_______Sử dụng toán tử
x = "Happy"
y = "Coding"
z = x + " " + y
print[z]
# Happy Coding
3, chúng tôi đã nhân đôi giá trị của chuỗi "Happy" ba lần với ba giá trị được in gần nhauNếu chúng ta thêm một khoảng trắng ở cuối chuỗi, thì các chuỗi sẽ được tách ra. Đó là
x = "Happy "
y = x * 3
print[y]
# Happy Happy Happy
Phần kết luận
Trong bài viết này, chúng ta đã học cách kết hợp các chuỗi trong Python thông qua phép nối
Cảm ơn bạn đã đọc và mã hóa vui vẻ
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Tiểu sử của tác giả này có thể được tìm thấy trong các bài viết của ông
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu
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 = """1 và{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]
class display[object]: """Display HTML representation of multiple objects""" template = """2 được xây dựng dành cho 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{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]
Ở đâ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 = """1 và{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]
class display[object]: """Display HTML representation of multiple objects""" template = """2 với hàm{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]
class display[object]: """Display HTML representation of multiple objects""" template = """5;{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]
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 = """2 của một hình thức cụ thể sẽ hữu ích bên dưới{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]
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 = """2 cạnh nhau. Mã này sử dụng phương thức{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]
class display[object]: """Display HTML representation of multiple objects""" template = """8 đặc biệt mà IPython sử dụng để triển khai hiển thị đối tượng phong phú của nó{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]
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
Nhớ lại. Ghép các mảng NumPy
Nối các đối tượng
class display[object]: """Display HTML representation of multiple objects""" template = """1 và{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]
class display[object]: """Display HTML representation of multiple objects""" template = """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{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]
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]1 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]
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]
Ra[4]
array[[1, 2, 3, 4, 5, 6, 7, 8, 9]]
Đối số đầu tiên là một danh sách hoặc bộ mảng để nối. Ngoài ra, nó cần một từ khóa
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]2 cho phép bạn chỉ định trục dọc theo đó kết quả sẽ được nối
Trong [5]
x = [[1, 2], [3, 4]] np.concatenate[[x, x], axis=1]
Ra[5]
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]
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]
5
Pandas có một hàm,
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]4, có cú pháp tương tự như hàm
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]1 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
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]4 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 = """1 hoặc{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]
class display[object]: """Display HTML representation of multiple objects""" template = """2, cũng như{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]
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]9 có thể được sử dụng cho nối mảng đơn giản
Trong [6]
ser1 = pd.Series[['A', 'B', 'C'], index=[1, 2, 3]] ser2 = pd.Series[['D', 'E', 'F'], index=[4, 5, 6]] pd.concat[[ser1, ser2]]
Ra[6]
1 A 2 B 3 C 4 D 5 E 6 F dtype: object
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 = """2s{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]
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, phép nối diễn ra theo hàng trong
class display[object]: """Display HTML representation of multiple objects""" template = """2 [i. e. ,{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]
array[[1, 2, 3, 4, 5, 6, 7, 8, 9]]2]. Giống như
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]1,
class display[object]: """Display HTML representation of multiple objects""" template = """5 cho phép xác định đặc điểm của một trục dọc theo đó quá trình nối sẽ diễn ra. Xem xét ví dụ sau{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]
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
array[[1, 2, 3, 4, 5, 6, 7, 8, 9]]5;
trùng lặp chỉ số
Một điểm khác biệt quan trọng giữa
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]1 và
class display[object]: """Display HTML representation of multiple objects""" template = """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{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]
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 11_______2 giây, nhưng kết quả thường không mong muốn.
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]4 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
x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] np.concatenate[[x, y, z]]4 không trùng nhau, bạn có thể chỉ định cờ
x = [[1, 2], [3, 4]] np.concatenate[[x, x], axis=1]2. 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ỉ số
Đô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ờ
x = [[1, 2], [3, 4]] np.concatenate[[x, x], axis=1]3. Với giá trị này được đặt thành true, phép nối sẽ tạo chỉ mục số nguyên mới cho kết quả là
class display[object]: """Display HTML representation of multiple objects""" template = """1{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]
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], ign_index=True]
AB0A0B01A1B12A2B23A3B3
Thêm khóa MultiIndex
Một tùy chọn khác là sử dụng tùy chọn
x = [[1, 2], [3, 4]] np.concatenate[[x, x], axis=1]5 để chỉ định nhãn cho các 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 = """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{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]
Kết nối với tham gia
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 = """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à{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]
class display[object]: """Display HTML representation of multiple objects""" template = """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{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]
class display[object]: """Display HTML representation of multiple objects""" template = """2 sau đây, chúng có một số [nhưng không phải tất cả]. ] cột chung{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]
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ố
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]0 và
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]1 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 [_______29_______2], 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
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]3
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ố
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]1, 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 = """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;{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]
Phương pháp array[[[1, 2, 1, 2],
[3, 4, 3, 4]]]
6
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 = """1 và{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]
class display[object]: """Display HTML representation of multiple objects""" template = """2 có một phương thức{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]
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]9 có thể thực hiện cùng một việc trong ít lần nhấn phím hơn. Ví dụ: thay vì gọi
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]0, bạn chỉ cần gọi
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]1
Trong [16]
class display[object]: """Display HTML representation of multiple objects""" template = """0{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]
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
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]6 và
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]3 trong danh sách Python, phương thức
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]6 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. Vì vậy, nếu bạn dự định thực hiện nhiều phép toán
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]9, thì tốt hơn hết là xây dựng một danh sách các
class display[object]: """Display HTML representation of multiple objects""" template = """2 và chuyển tất cả chúng cùng một lúc cho hàm{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]
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]7
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, kết hợp/kết hợp kiểu cơ sở dữ liệu được triển khai trong
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]8. Để biết thêm thông tin về
# Signature in Pandas v0.18 pd.concat[objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True]7,
array[[[1, 2, 1, 2], [3, 4, 3, 4]]]6 và chức năng liên quan, hãy xem phần "Hợp nhất, Tham gia và Nối" trong tài liệu Pandas