Bộ dữ liệu nhỏ [ 150Rows]
df['Year'].astype[str] + df['quarter']
CẬP NHẬT: Thời gian biểu đồ Pandas 0.23.4 Timing graph Pandas 0.23.4
Hãy thử nghiệm nó trên 200k Rows DF:
In [250]: df
Out[250]:
Year quarter
0 2014 q1
1 2015 q2
In [251]: df = pd.concat[[df] * 10**5]
In [252]: df.shape
Out[252]: [200000, 2]
CẬP NHẬT: Thời gian mới sử dụng Pandas 0.19.0 new timings using Pandas 0.19.0
Thời gian mà không tối ưu hóa CPU/GPU [được sắp xếp từ nhanh nhất đến chậm nhất]: without CPU/GPU optimization [sorted from fastest to slowest]:
In [107]: %timeit df['Year'].astype[str] + df['quarter']
10 loops, best of 3: 131 ms per loop
In [106]: %timeit df['Year'].map[str] + df['quarter']
10 loops, best of 3: 161 ms per loop
In [108]: %timeit df.Year.str.cat[df.quarter]
10 loops, best of 3: 189 ms per loop
In [109]: %timeit df.loc[:, ['Year','quarter']].astype[str].sum[axis=1]
1 loop, best of 3: 567 ms per loop
In [110]: %timeit df[['Year','quarter']].astype[str].sum[axis=1]
1 loop, best of 3: 584 ms per loop
In [111]: %timeit df[['Year','quarter']].apply[lambda x : '{}{}'.format[x[0],x[1]], axis=1]
1 loop, best of 3: 24.7 s per loop
Thời gian bằng cách sử dụng tối ưu hóa CPU/GPU: using CPU/GPU optimization:
In [113]: %timeit df['Year'].astype[str] + df['quarter']
10 loops, best of 3: 53.3 ms per loop
In [114]: %timeit df['Year'].map[str] + df['quarter']
10 loops, best of 3: 65.5 ms per loop
In [115]: %timeit df.Year.str.cat[df.quarter]
10 loops, best of 3: 79.9 ms per loop
In [116]: %timeit df.loc[:, ['Year','quarter']].astype[str].sum[axis=1]
1 loop, best of 3: 230 ms per loop
In [117]: %timeit df[['Year','quarter']].astype[str].sum[axis=1]
1 loop, best of 3: 230 ms per loop
In [118]: %timeit df[['Year','quarter']].apply[lambda x : '{}{}'.format[x[0],x[1]], axis=1]
1 loop, best of 3: 9.38 s per loop
Trả lời đóng góp của @Anton-VBR
Bạn có thể sử dụng cú pháp sau để kết hợp hai cột văn bản thành một trong gấu trúc DataFrame:
df['new_column'] = df['column1'] + df['column2']
Nếu một trong các cột không phải là một chuỗi, bạn có thể chuyển đổi nó bằng lệnh ASTYPE [STR]:astype[str] command:
df['new_column'] = df['column1'].astype[str] + df['column2']
Và bạn có thể sử dụng cú pháp sau để kết hợp nhiều cột văn bản thành một:
df['new_column'] = df[['col1', 'col2', 'col3', ...]].agg[' '.join, axis=1]
Các ví dụ sau đây cho thấy cách kết hợp các cột văn bản trong thực tế.
Ví dụ 1: Kết hợp hai cột
Mã sau đây cho thấy cách kết hợp hai cột văn bản thành một trong gấu trúc DataFrame:
import pandas as pd #create dataFrame df = pd.DataFrame[{'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'], 'first': ['Dirk', 'Kobe', 'Tim', 'Lebron'], 'last': ['Nowitzki', 'Bryant', 'Duncan', 'James'], 'points': [26, 31, 22, 29]}] #combine first and last name column into new column, with space in between df['full_name'] = df['first'] + ' ' + df['last'] #view resulting dataFrame df team first last points full_name 0 Mavs Dirk Nowitzki 26 Dirk Nowitzki 1 Lakers Kobe Bryant 31 Kobe Bryant 2 Spurs Tim Duncan 22 Tim Duncan 3 Cavs Lebron James 29 Lebron James
Chúng tôi đã tham gia cột đầu tiên và tên cuối cùng với một khoảng trống ở giữa, nhưng chúng tôi cũng có thể sử dụng một dấu phân cách khác như dấu gạch ngang:
df.Year.str.cat[df.quarter]
0Ví dụ 2: Chuyển đổi thành văn bản và kết hợp hai cột
Mã sau đây cho thấy cách chuyển đổi một cột thành văn bản, sau đó tham gia vào một cột khác:
df.Year.str.cat[df.quarter]
1Ví dụ 3: Kết hợp nhiều hơn hai cột
Mã sau đây cho thấy cách tham gia nhiều cột vào một cột:
df.Year.str.cat[df.quarter]
2Tài nguyên bổ sung
Pandas: Cách tìm sự khác biệt giữa hai cột Pandas: Cách tìm sự khác biệt giữa hai hàng Pandas: Cách sắp xếp các cột theo tên
Pandas: How to Find the Difference Between Two Rows
Pandas: How to Sort Columns by Name