替换不正确删除对象变量类型

I'm trying to remove the ":30" portion of values in my First variable. The First variable data type is object.

Here are a few examples of of the First variable, and the counts, ignore the counts:

11a       211
7p        178
4p        127
2:30p     112
11:30a    108
1p        107
12p       105
9a        100
10p        85
2p         24
10:30a     12
6p          5
9:30a       2
9p          2
12:30a      2
8p          2

I wrote the following code which runs without any errors; however, when I run the value counts, it still shows times with a ":30". The NewFirst variable dataype is int64.Not quite sure what I'm doing wrong here.

    bad_chars = ":30"
DF["NewFirst"] = DF.First.replace(bad_chars,'')
DF["NewFirst"].value_counts()

The desired output would have the NewFirst values like:

11a       211
7p        178
4p        127
2p        112
11a       108
1p        107
12p       105
9a        100
10p        85
2p         24
10a        12
6p          5
9a          2
9p          2
12a        2
8p          2
评论
  • 猪一样队友
    猪一样队友 回复

    You shouldn't be looping over the characters in bad_chars. That will remove all 3 and 0 characters, so 10p will become 1p, and 3a will become a.

    You should just replace the whole bad_chars string, with no loop:

    DF["NewFirst"] = DF.First.replace(bad_chars,'')