替换 python 中数据框列中的值不起作用
Replace value in dataframe column in python not working
我有这个 pandas 数据框:
Score # Bads # Goods
(470, 480] 3 2
(480, 490] 5 6
(490, 500] 20 24
(500, 510] 63 99
(510, 520] 176 230
(520, 530] 555 825
(530, 540] 1272 2105
(540, 550] 3298 7084
(550, 560] 7559 18819
(560, 570] 11293 31546
(570, 580] 7475 24476
(580, 590] 7982 31377
(590, 600] 4699 22787
(600, 610] 3125 18899
(610, 620] 1741 11193
(620, 630] 771 6553
(630, 640] 346 3603
(640, 650] 144 1756
(650, 660] 56 778
(660, 670] 23 277
(670, 680] 5 84
(680, 690] 0 18
(690, 700] 0 2
我需要用符号“-<”替换分数列中的逗号 (",")。
我已经编码了:
df['Score'] = df['Score'].replace(',', "-<")
print(df)
但逗号根本没有被替换:它们保留在数据框中。
Score # Bads # Goods
(470, 480] 3 2
(480, 490] 5 6
(490, 500] 20 24
(500, 510] 63 99
(510, 520] 176 230
(520, 530] 555 825
(530, 540] 1272 2105
(540, 550] 3298 7084
(550, 560] 7559 18819
(560, 570] 11293 31546
(570, 580] 7475 24476
(580, 590] 7982 31377
(590, 600] 4699 22787
(600, 610] 3125 18899
(610, 620] 1741 11193
(620, 630] 771 6553
(630, 640] 346 3603
(640, 650] 144 1756
(650, 660] 56 778
(660, 670] 23 277
(670, 680] 5 84
(680, 690] 0 18
(690, 700] 0 2
我不明白为什么上面的代码不起作用。
您错过了 .str
部分:
df['Score'] = df['Score'].str.replace(',', "-<")
我有这个 pandas 数据框:
Score # Bads # Goods
(470, 480] 3 2
(480, 490] 5 6
(490, 500] 20 24
(500, 510] 63 99
(510, 520] 176 230
(520, 530] 555 825
(530, 540] 1272 2105
(540, 550] 3298 7084
(550, 560] 7559 18819
(560, 570] 11293 31546
(570, 580] 7475 24476
(580, 590] 7982 31377
(590, 600] 4699 22787
(600, 610] 3125 18899
(610, 620] 1741 11193
(620, 630] 771 6553
(630, 640] 346 3603
(640, 650] 144 1756
(650, 660] 56 778
(660, 670] 23 277
(670, 680] 5 84
(680, 690] 0 18
(690, 700] 0 2
我需要用符号“-<”替换分数列中的逗号 (",")。 我已经编码了:
df['Score'] = df['Score'].replace(',', "-<")
print(df)
但逗号根本没有被替换:它们保留在数据框中。
Score # Bads # Goods
(470, 480] 3 2
(480, 490] 5 6
(490, 500] 20 24
(500, 510] 63 99
(510, 520] 176 230
(520, 530] 555 825
(530, 540] 1272 2105
(540, 550] 3298 7084
(550, 560] 7559 18819
(560, 570] 11293 31546
(570, 580] 7475 24476
(580, 590] 7982 31377
(590, 600] 4699 22787
(600, 610] 3125 18899
(610, 620] 1741 11193
(620, 630] 771 6553
(630, 640] 346 3603
(640, 650] 144 1756
(650, 660] 56 778
(660, 670] 23 277
(670, 680] 5 84
(680, 690] 0 18
(690, 700] 0 2
我不明白为什么上面的代码不起作用。
您错过了 .str
部分:
df['Score'] = df['Score'].str.replace(',', "-<")