Bạn có thể sử dụng
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
6 giống như bạn sẽ sử dụng >>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
7 bình thường. Chỉ cần phân chia trên chuỗi >>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
8 và lập chỉ mục danh sách được tạo từ phương thức >>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
7:>>> df = pd.DataFrame[{'text': ["vendor a::ProductA", "vendor b::ProductA", "vendor a::Productb"]}]
>>> df
text
0 vendor a::ProductA
1 vendor b::ProductA
2 vendor a::Productb
>>> df['text_new'] = df['text'].str.split['::'].str[0]
>>> df
text text_new
0 vendor a::ProductA vendor a
1 vendor b::ProductA vendor b
2 vendor a::Productb vendor a
Đây là một giải pháp không phải của PANDAS:
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
Chỉnh sửa: Đây là lời giải thích từng bước về những gì đang xảy ra trong
# Select the pandas.Series object you want
>>> df['text']
0 vendor a::ProductA
1 vendor b::ProductA
2 vendor a::Productb
Name: text, dtype: object
# using pandas.Series.str allows us to implement "normal" string methods
# [like split] on a Series
>>> df['text'].str
# Now we can use the split method to split on our '::' string. You'll see that
# a Series of lists is returned [just like what you'd see outside of pandas]
>>> df['text'].str.split['::']
0 [vendor a, ProductA]
1 [vendor b, ProductA]
2 [vendor a, Productb]
Name: text, dtype: object
# using the pandas.Series.str method, again, we will be able to index through
# the lists returned in the previous step
>>> df['text'].str.split['::'].str
# now we can grab the first item in each list above for our desired output
>>> df['text'].str.split['::'].str[0]
0 vendor a
1 vendor b
2 vendor a
Name: text, dtype: object
0 ở trên:# Select the pandas.Series object you want
>>> df['text']
0 vendor a::ProductA
1 vendor b::ProductA
2 vendor a::Productb
Name: text, dtype: object
# using pandas.Series.str allows us to implement "normal" string methods
# [like split] on a Series
>>> df['text'].str
# Now we can use the split method to split on our '::' string. You'll see that
# a Series of lists is returned [just like what you'd see outside of pandas]
>>> df['text'].str.split['::']
0 [vendor a, ProductA]
1 [vendor b, ProductA]
2 [vendor a, Productb]
Name: text, dtype: object
# using the pandas.Series.str method, again, we will be able to index through
# the lists returned in the previous step
>>> df['text'].str.split['::'].str
# now we can grab the first item in each list above for our desired output
>>> df['text'].str.split['::'].str[0]
0 vendor a
1 vendor b
2 vendor a
Name: text, dtype: object
Tôi sẽ đề nghị kiểm tra các tài liệu pandas.series.str, hoặc, tốt hơn là làm việc với dữ liệu văn bản trong gấu trúc.
Khi làm việc với các bộ dữ liệu trong thế giới thực trong Python và Pandas, bạn sẽ cần xóa các ký tự khỏi chuỗi của mình *rất nhiều *. Tôi thấy ba phương pháp này có thể giải quyết rất nhiều vấn đề của bạn:
.split[] #splits the string into two tuples around whatever character it was given and deletes that character..lstrip[] #strips everything before and including the character or set of characters you say. If left blank, deletes whitespace..rstrip[] #strips everything out from the end up to and including the character or set of characters you give. If left blank, deletes whitespace at the end.
Các tài liệu Python là ở đây.
Hãy cùng đi qua cách chúng ta có thể sử dụng các lệnh này. Tôi sẽ sử dụng bộ dữ liệu Titanic trên Kaggle và tôi muốn lấy tên đầu tiên từ mỗi hành khách.
Tên đầu tiên trong danh sách là:
'Braund, Mr. Owen Harris'
Chúng tôi chỉ muốn cái tên của Ow Owen vì chúng tôi chỉ quan tâm đến tên đầu tiên. Mỗi tên nam trong danh sách hành khách đều có mô hình sau [ngoại trừ phụ nữ, chúng tôi sẽ nhận được điều đó một chút]:
Họ, chào. First_name last_name
Dưới đây là giải pháp trong một dòng mã, sau đó, tôi sẽ chia nhỏ từng bước.
name = 'Braund, Mr. Owen Harris'
first_name = name.split['.'][1].lstrip[].split[' '][0]
Điều đó trở lại:
'Owen'
Đó là rất nhiều mã! Hãy cùng bước đi qua các lệnh đó từng bước.
Vì mỗi tên đều chứa một lời chào, và do đó, một khoảng thời gian, trước tên đầu tiên, chúng ta có thể thoát khỏi mọi thứ lên đến và trước khoảng thời gian. Vì vậy, trước tiên, chúng tôi chia tên thành hai chuỗi.
splitting = name.split['.']
print[splitting]
['Braund, Mr', ' Owen Harris']
Chúng tôi chỉ muốn ‘Owen Harris, phần đầu tiên đó là vô dụng. Vì vậy, tôi chỉ gọi chuỗi thứ hai trong danh sách, ở vị trí chỉ mục 1.
First_Last = splitting[1]
print[First_Last]
' Owen Harris'
Nhưng chúng tôi không muốn không gian trống xấu xí đó ngay từ đầu. Đó là lý do tại sao chúng tôi sử dụng .lstrip []. Trong trường hợp này, chúng tôi chỉ muốn xóa không gian trống, vì vậy không cần phải vượt qua bất kỳ đối số nào.
First_Last = First_Last.lstrip[]
print[First_Last]
'Owen Harris'
Bây giờ chúng tôi muốn thoát khỏi họ. Chúng tôi làm điều đó bằng cách sử dụng .Split [] một lần nữa. Lần này dễ dàng hơn vì luôn có một khoảng trống giữa tên đầu tiên và họ. Bởi vì .Split [] loại bỏ nhân vật mà Lừa được thông qua, điều đó có nghĩa là chúng ta sẽ chỉ còn lại chỉ với tên đầu tiên và tên cuối cùng, không có khoảng trống. Từ đó chúng tôi chỉ muốn mục đầu tiên, ở vị trí chỉ mục 0.
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
0Vì vậy, một lần nữa, đặt toàn bộ mọi thứ lại với nhau thành một dòng.
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
1Tiền thưởng: .rstrip []
Vì vậy, điều đó giải quyết vấn đề cho những người đàn ông trong bộ dữ liệu, nhưng bây giờ hãy để Lôi nhìn vào tên nữ. Họ trông như thế này:
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
2Vì vậy, cái tên chúng tôi muốn ở đây là Florence, đó là tên đầu tiên của hành khách, nhưng cô ấy được liệt kê dưới tên chồng của cô ấy. Ở đây, cách chúng ta có được tên của cô ấy:
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
3Điều đó khiến chúng ta đến với Briggs Thayer]
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
4Điều đó đưa chúng ta đến ‘Florence Briggs Thayer, vì .rstrip [] hoạt động như .lstrip [] nhưng ở cuối từ.
>>> df['text_new1'] = [x.split['::'][0] for x in df['text']]
>>> df
text text_new text_new1
0 vendor a::ProductA vendor a vendor a
1 vendor b::ProductA vendor b vendor b
2 vendor a::Productb vendor a vendor a
5