Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从dataframe中的列中删除某些字符串_Python_Python 3.x_Pandas_String - Fatal编程技术网

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

Python 如何从dataframe中的列中删除某些字符串,python,python-3.x,pandas,string,Python,Python 3.x,Pandas,String,我想从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 YEAR WEEK 2019 1 2019 2 2019 3 2019 14 2019 25 2020 6 2020 7 我想从WEEK列中删除WK

我想从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
YEAR    WEEK
2019    1
2019    2
2019    3
2019    14
2019    25
2020    6
2020    7
我想从
WEEK
列中删除
WK-
0
,以便我的输出如下所示:

YEAR    WEEK
2019    WK-01
2019    WK-02
2019    WK-03
2019    WK-14
2019    WK-25
2020    WK-06
2020    WK-07
YEAR    WEEK
2019    1
2019    2
2019    3
2019    14
2019    25
2020    6
2020    7
您可以尝试:

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

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

去掉前三个字符并转换为int

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

使用
str.extract
和仅使用
str
有什么不同?@NurAtiqah
str.extract
可以从正则表达式中提取匹配项
str[…]
允许对特定元素或元素片段进行矢量化访问。注意…所以..如果我要将此输出与具有多列的原始数据帧相结合…我可以使用
join()
对吗?