简体   繁体   English

如何从 pandas dataframe 中的列中删除某些字符串

[英]How to remove certain string from column in pandas dataframe

I want to remove a certain keywords or string in a column from pandas dataframe.我想从 pandas dataframe 中删除列中的某些关键字或字符串。

The dataframe df looks like this: dataframe df看起来像这样:

YEAR    WEEK
2019    WK-01
2019    WK-02
2019    WK-03
2019    WK-14
2019    WK-25
2020    WK-06
2020    WK-07

I would like to remove WK- and 0 from the WEEK column so that my output will looks like this:我想从WEEK列中删除WK-0 ,以便我的 output 看起来像这样:

YEAR    WEEK
2019    1
2019    2
2019    3
2019    14
2019    25
2020    6
2020    7

You can try:你可以试试:

df['WEEK'] = df['WEEK'].str.extract('(\d*)$').astype(int)

Output: Output:

   YEAR  WEEK
0  2019     1
1  2019     2
2  2019     3
3  2019    14
4  2019    25
5  2020     6
6  2020     7

Shave off the first three characters and convert to int.去掉前三个字符并转换为 int。

df['WEEK'] = df['WEEK'].str[3:].astype(int)

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

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