Chúng tôi có thể có dữ liệu mà không có thông tin, nhưng chúng tôi không thể có thông tin mà không có dữ liệu. & nbsp; Trích dẫn này đẹp làm sao. Dữ liệu là xương sống của nhà khoa học dữ liệu và theo một nhà khoa học dữ liệu khảo sát dành khoảng 60% thời gian để làm sạch và tổ chức dữ liệu, do đó, trách nhiệm của chúng tôi là làm quen với các kỹ thuật khác nhau để tổ chức dữ liệu theo cách tốt hơn. Trong bài viết này, chúng tôi sẽ tìm hiểu về các phương pháp khác nhau để loại bỏ khoảng trắng dải thêm khỏi toàn bộ khung dữ liệu. Bộ dữ liệu được sử dụng ở đây được đưa ra dưới đây:
Trong hình trên, chúng tôi đang quan sát rằng bên trong tên, tuổi, nhóm máu và các cột giới tính, dữ liệu theo cách bất thường. Trong hầu hết các ô của một cột cụ thể, thêm khoảng trắng có mặt trong phần hàng đầu của các giá trị. Vì vậy, mục đích của chúng tôi là loại bỏ tất cả các khoảng trắng thêm và sắp xếp nó một cách có hệ thống. Chúng tôi sẽ sử dụng các phương pháp khác nhau sẽ giúp chúng tôi loại bỏ tất cả không gian thêm khỏi ô. Các phương pháp khác nhau là: & nbsp;
Using Strip[] function Using Skipinitialspace Using replace function Using Converters
Các phương pháp khác nhau để loại bỏ thêm khoảng trắng
Phương pháp 1: Sử dụng hàm Dải []: & nbsp;
Pandas cung cấp phương thức xác định trước pandas.series.str.strip [] để loại bỏ khoảng trắng khỏi chuỗi. Sử dụng chức năng dải, chúng ta có thể dễ dàng loại bỏ thêm khoảng trắng khỏi khoảng trắng dẫn đầu và theo dõi khỏi nhìn chằm chằm. Nó trả về một loạt hoặc chỉ mục của một đối tượng. Nó lấy tập hợp các ký tự mà chúng ta muốn loại bỏ khỏi đầu và đuôi của chuỗi [nhân vật dẫn đầu và dấu vết]. Theo mặc định, không có và nếu chúng ta không vượt qua bất kỳ ký tự nào thì nó sẽ loại bỏ khoảng trắng dẫn đầu và theo dõi khỏi chuỗi. Nó trả về một loạt hoặc chỉ mục của một đối tượng.“pandas.Series.str.strip[]” to remove the whitespace from the string. Using strip function we can easily remove extra whitespace from leading and trailing whitespace from staring. It returns a series or index of an object. It takes set of characters that we want to remove from head and tail of string[leading and trailing character’s]. By default, it is none and if we do not pass any characters then it will remove leading and trailing whitespace from the string. It returns a series or index of an object.
Cú pháp: pandas.series.str.strip [to_strip = none]pandas.Series.str.strip[to_strip = None]
Giải thích: Phải mất tập hợp các ký tự mà chúng tôi muốn loại bỏ khỏi đầu và đuôi của chuỗi [nhân vật dẫn đầu và dấu vết].It takes set of characters that we want to remove from head and tail of string[leading and trailing character’s].
Tham số: Theo mặc định, đó là không và nếu chúng ta không vượt qua bất kỳ ký tự nào thì nó sẽ loại bỏ khoảng trắng dẫn đầu và theo dõi khỏi chuỗi. Nó trả về loạt hoặc chỉ mục của đối tượng. & Nbsp; By default it is none and if we do not pass any characters then it will remove leading and trailing whitespace from the string. It returns series or index of object.
Ví dụ: & nbsp;
Python3
import
pandas as pd
df
=
pd.DataFrame[{
'Names'
Các
Các
=
1=
2 : [
=
4
Syntax : pandas.Series.str.replace[' ', '']0____54____10____
=
8Syntax : pandas.Series.str.replace[' ', '']0____58____1010____58____1010444565
pd.DataFrame[{
6pd.DataFrame[{
7
pd.DataFrame[{
8'Names'
'Names'
0'Names'
1'Names'
2
pd.DataFrame[{
8pandas as pd
7'Names'
0'Names'
1'Names'
2
Các
: [
3: [
4
Output: Output:
Phương pháp 2: Sử dụng Skipinitialspace: & nbsp; & nbsp;
Nó không phải là bất kỳ phương thức nào nhưng nó là một trong những tham số có bên trong phương thức read_csv [] có trong gấu trúc. Bên trong pandas.read_csv [] Phương thức SkipinitialSpace tham số có mặt bằng cách sử dụng mà chúng ta có thể bỏ qua không gian ban đầu có mặt trong toàn bộ khung dữ liệu của chúng ta. Theo mặc định, nó là sai, làm cho nó đúng để xóa thêm không gian. & Nbsp;pandas.read_csv[] method skipinitialspace parameter is present using which we can skip initial space present in our whole dataframe. By default, it is False, make it True to remove extra space.
Cú pháp: pandas.read_csv [‘path_of_csv_file, Skipinitialspace = true]pandas.read_csv[‘path_of_csv_file’, skipinitialspace = True]
& nbsp;# Theo giá trị mặc định của SkipInitialSpace là sai, làm cho nó đúng khi sử dụng tham số này.
Ví dụ: & nbsp;
Python3
import
pandas as pd
df
=
pd.DataFrame[{
'Names'
: [
3: [
4
Output:
Output:
Các 3: Using replace function :
Các“pandas.Series.str.replace[]” to remove whitespace. Its program will be same as strip[] method program only one difference is that here we will use replace function at the place of strip[].
Syntax : pandas.Series.str.replace[' ', '']
Ví dụ: & nbsp;
Python3
import
pandas as pd
df
=
pd.DataFrame[{
'Names'
Các
Các
=
1=
2 : [
=
4
Syntax : pandas.Series.str.replace[' ', '']0____54____10____
=
8Syntax : pandas.Series.str.replace[' ', '']0____58____1010____58____1010444565
pd.DataFrame[{
6pd.DataFrame[{
7
pd.DataFrame[{
8'Names'
'Names'
0'Names'
1
Syntax : pandas.Series.str.replace[' ', '']67
Syntax : pandas.Series.str.replace[' ', '']68
Syntax : pandas.Series.str.replace[' ', '']69
Các
pd.DataFrame[{
8=
2'Names'
0'Names'
1
Syntax : pandas.Series.str.replace[' ', '']67
Syntax : pandas.Series.str.replace[' ', '']68
Syntax : pandas.Series.str.replace[' ', '']69
: [
3: [
4
Output:
Phương pháp 4: Sử dụng bộ chuyển đổi:
Nó tương tự như Skipinitialspace, nó là một trong những tham số có bên trong phương thức định nghĩa pandas tên là READ_CSV. Nó được sử dụng để áp dụng các chức năng khác nhau trên các cột cụ thể. Chúng ta phải vượt qua các chức năng trong từ điển. Ở đây chúng tôi sẽ vượt qua hàm Dải [] trực tiếp sẽ loại bỏ không gian thêm trong khi đọc tệp CSV.
Cú pháp: pd.read_csv [PATH PATH_OF_FILE, bộ chuyển đổi = {‘cột_names,: function_name}] pd.read_csv[“path_of_file”, converters={‘column_names’: function_name}]
# Vượt qua các hàm của các hàm và tên cột, trong đó tên cột hoạt động như các khóa và chức năng duy nhất là giá trị. & nbsp; & nbsp;
Ví dụ: & nbsp;
Python3
import
pandas as pd
df
=
: [
9
Using Strip[] function Using Skipinitialspace Using replace function Using Converters0
Syntax : pandas.Series.str.replace[' ', '']92
=
__194____10222Syntax : pandas.Series.str.replace[' ', '']99
pandas as pd
7 Syntax : pandas.Series.str.replace[' ', '']96
'Names'
1Syntax : pandas.Series.str.replace[' ', '']98
Syntax : pandas.Series.str.replace[' ', '']99
=
2 Syntax : pandas.Series.str.replace[' ', '']96
'Names'
1import
08: [
3: [
4
Output:
Loại bỏ thêm khoảng trắng khỏi toàn bộ dữ liệu bằng cách tạo một số mã: & nbsp;
Python3
import
pandas as pd
df
=
: [
9
Using Strip[] function Using Skipinitialspace Using replace function Using Converters0
Syntax : pandas.Series.str.replace[' ', '']92
=
__194____10222import
24
Syntax : pandas.Series.str.replace[' ', '']5
import
19Syntax : pandas.Series.str.replace[' ', '']7
import
19Syntax : pandas.Series.str.replace[' ', '']9
import
0=
1import
2: [
import
4import
19import
6import
19import
4import
19pandas as pd
0import
19pandas as pd
2import
19pandas as pd
4import
0
=
1import
47: [
pandas as pd
9import
19df
1import
19df
3import
19df
5
Syntax : pandas.Series.str.replace[' ', '']0
import
57df
7import
19df
9import
0
Syntax : pandas.Series.str.replace[' ', '']99
pandas as pd
7 Syntax : pandas.Series.str.replace[' ', '']96
'Names'
1Syntax : pandas.Series.str.replace[' ', '']98
=
1pd.DataFrame[{
7
Syntax : pandas.Series.str.replace[' ', '']99
=
2 Syntax : pandas.Series.str.replace[' ', '']96
'Names'
1import
08Loại bỏ thêm khoảng trắng khỏi toàn bộ dữ liệu bằng cách tạo một số mã: & nbsp;
df
=
pd.DataFrame[{
'Names'
: [
' Sunny'
import
19
Syntax : pandas.Series.str.replace[' ', '']1____219
Syntax : pandas.Series.str.replace[' ', '']3
Syntax : pandas.Series.str.replace[' ', '']0
=
1=
22___
import
86pandas as pd
02import
92
import
93pandas as pd
05
pandas as pd
06
: [
3: [
4
import
79 import
80‘Names’, ‘Age’, ‘Blood_Group’ and ‘Gender’. Almost all columns having irregular data. Now the major part begin from here, we created a function which will remove extra leading and trailing whitespace from the data. This function taking dataframe as a parameter and checking datatype of each column and if datatype of column is ‘Object’ then apply strip function which is
predefined in pandas library on that column else it will do nothing. Then in next line we apply whitespace_remover[] function on the dataframe which successfully remove the extra whitespace from the columns.
Output: