简体   繁体   English

如何在python中仅提取字符串的指定部分

[英]How to extract only specified part of string in python

I have a situation where one of my column is bike name that contains value like RoyalEnfeild Classic 350 I want only the company name ie RoyalEnfeild to be replace in that column.我有一种情况,我的一列是自行车名称,其中包含像 RoyalEnfeild Classic 350 这样的值,我只想在该列中替换公司名称,即 RoyalEnfeild。

so that all the values of that particular column should be changed with the new string以便使用新字符串更改该特定列的所有值

For example:例如:

old table is like --旧桌子就像——

Bike Name 
RoyalEnfeild Classic 350
RoyalEnfeild Classic 350
KTM dUKE 390

NEW Table新表

Bike Name
RoyalEnfeild
RoyalEnfeild 
KTM 

I am new to python can anyone help me with the answer ?我是 python 新手,谁能帮我解答?

For this example try:对于此示例,请尝试:

 df['Bike Name'].str.extract('(\w+)')

Output:输出:

              0
0  RoyalEnfeild
1  RoyalEnfeild
2           KTM

You can use string slicing in case it's a string.Or if it's an array then you can access that element and assingn it to the whatever string you want to assign.如果它是一个字符串,您可以使用字符串切片。或者如果它是一个数组,那么您可以访问该元素并将其分配给您想要分配的任何字符串。 arrays string slicing数组字符串切片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM