Làm cách nào để trích xuất một cột trong python?

def extract_columns(self, result={}, selector='', table_headers=[], attr='', connector='', default='', verbosity=0, *args, **kwargs): """ Column data extraction for extract_tabular """ result_list = [] try: if type(selector) in [str, unicode]: selectors = [selector] elif type(selector) == list: selectors = selector[:] else: raise Exception("Use a list of selector expressions for the various columns") from itertools import izip, count pairs = izip(table_headers, selectors) columns = {} for head, selector in pairs: columns[head] = self.get_tree_tag(selector) try: for i in count(start=0): r = result.copy() for head in columns.keys(): if verbosity > 1: print("\nExtracting", head, "attribute", sep=' ', end='') col = columns[head][i] if attr == "text": try: content = connector.join([make_ascii(x).strip() for x in col.itertext()]) except Exception: content = default content = content.replace("\n", " ").strip() else: content = col.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) r[head] = content result_list.append(r) except IndexError: pass except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except TypeError: raise Exception("Selector expression string to be provided. Got " + selector) return result_list

Bây giờ, chúng ta sẽ xem cách chúng ta có thể lấy chuỗi con cho tất cả các giá trị của một cột trong khung dữ liệu Pandas. Trích xuất này có thể rất hữu ích khi làm việc với dữ liệu. Ví dụ: chúng tôi có tên và họ của những người khác nhau trong một cột và chúng tôi cần trích xuất 3 chữ cái đầu tiên trong tên của họ để tạo tên người dùng của họ

ví dụ 1
Chúng ta có thể lặp qua phạm vi của cột và tính chuỗi con cho từng giá trị trong cột




# importing pandas as pd

import pandas as pd 

 

# creating a dictionary

dict = {'Name':[import0_______2_______1import2import3

import4_______2_______5_______2_______1import7import8

 

pandas as pd 0

pandas as pd 1

pandas as pd 2_______7_______ pandas as pd 4dictpandas as pd 6

 

pandas as pd 8

pandas as pd 9  0 1  2 3 4import1 6 7

 8 9= # creating a dictionary1# creating a dictionary2_______5_______3

 

pandas as pd 2

đầu ra

Ghi chú. Để biết thêm thông tin, hãy tham khảo Python Trích xuất các hàng bằng Pandas

ví dụ 2. Trong ví dụ này, chúng tôi sẽ sử dụng # creating a dictionary6




# importing pandas as pd

import pandas as pd 

 

# creating a dictionary

dict = {'Name':[import0_______2_______1import2import1

import4_______2_______5_______2_______1import7import8

 

=7

pandas as pd 1

pandas as pd 2_______7_______ pandas as pd 4dictpandas as pd 6

 

{5

{6{7# creating a dictionary3_______7_______ {6'Name'9_______2'Name'3'Name'4'Name'5 3 4import1# creating a dictionary2pandas as pd 6

 

pandas as pd 2

đầu ra

ví dụ 3. Chúng ta cũng có thể sử dụng bộ truy cập str theo cách khác bằng cách sử dụng dấu ngoặc vuông




# importing pandas as pd

import pandas as pd 

 

# creating a dictionary

dict = {'Name':[import0_______2_______1import2import3

import4_______2_______5_______2_______1import7import8

 

import13

pandas as pd 2_______7_______ pandas as pd 4dictpandas as pd 6

 

{5

{6{7# creating a dictionary3_______7_______ {6'Name'9_______2'Name'3import29# creating a dictionary2# creating a dictionary3

 

pandas as pd 2

đầu ra

Ví dụ 4. Chúng ta cũng có thể sử dụng str. giải nén cho nhiệm vụ này. Trong ví dụ này, chúng tôi sẽ lưu trữ họ của từng người trong cột “LastName”

Chủ đề