如何从熊猫数据框中的列中删除某些字符串

时间:2020-06-25 13:45:26

标签: python python-3.x pandas string

我想从熊猫数据框中删除列中的某些关键字或字符串。

数据帧df如下所示:

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

我想从WK-列中删除0WEEK,以便我的输出看起来像这样:

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

2 个答案:

答案 0 :(得分:3)

您可以尝试:

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

答案 1 :(得分:3)

删除前三个字符并转换为int。

const Statistics = ({total, good, neutral, bad}) => {

return (
 <div>
   <Statistic text = "good" val = {good.toFixed(2)} />
   <Statistic text = "neutral" val = {neutral.toFixed(2)} />
   <Statistic text = "bad" val = {bad.toFixed(2)} />
   <Statistic text = "total" val = {total.toFixed(2)} />
   <Statistic text = "average" val = {((good - bad) / total).toFixed(2)} />
   <Statistic text = "positive" val = {(good / total).toFixed(2) + "%"} />
 </div>
 )
}