pandas 如何使用索引更改行的值?

g6ll5ycj  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(144)

我已经抓取了crypto.com网站,以DataFrame的形式获得了加密硬币的当前价格,它对Pandas的效果很好,但是“价格”的值是混合的。
输出如下:

Name                           Price 24H CHANGE
0             BBitcoinBTC      16.678,36$16.678,36+0,32%     +0,32%
1            EEthereumETH        $1.230,40$1.230,40+0,52%     +0,52%
2             UTetherUSDT                $1,02$1,02-0,01%     -0,01%
3                 BBNBBNB            $315,46$315,46-0,64%     -0,64%
4           UUSD CoinUSDC                $1,00$1,00+0,00%     +0,00%
5        BBinance USDBUSD                $1,00$1,00+0,00%     +0,00%
6                 XXRPXRP            $0,4067$0,4067-0,13%     -0,13%
7           DDogecoinDOGE           $0,1052$0,1052+13,73%    +13,73%
8             ACardanoADA            $0,3232$0,3232+0,98%     +0,98%
9           MPolygonMATIC            $0,8727$0,8727+1,20%     +1,20%
10           DPolkadotDOT                $5,48$5,48+0,79%     +0,79%

我创建了一个正则表达式来过滤混合日期:

import re

pattern = re.compile(r'(\$.*)(\$)')
for value in df['Price']:
    value = pattern.search(value)
    print(value.group(1))

输出:

$16.684,53
$1.230,25
$1,02
$315,56
$1,00
$1,00
$0,4078
$0,105
$0,3236
$0,8733

但我找不到改变值的方法。哪种方法最好呢?谢谢。

0md85ypi

0md85ypi1#

如果正则表达式是好,这将工作

df['Price']= df['Price'].apply(lambda x: pattern.search(x).group(1))
tez616oj

tez616oj2#

你能试试这个吗:

df['price_v2']=df['Price'].apply(lambda x: '$' + x.split('$')[1])

'''
0     $16.678,36+0,32%
1            $1.230,40
2                $1,02
3              $315,46
4                $1,00
5                $1,00
6              $0,4067
7              $0,1052
8              $0,3232
9              $0,8727
10               $5,48
Name: price, dtype: object

另外,BTC看起来和其他的不一样。这是你打错了还是api的回应?如果有看起来像BTC的奇偶校验,我们可以在代码中添加if else块:

df['price']=df['Price'].apply(lambda x: '$' + x.split('$')[1] if x.startswith('$') else '$' + x.split('$')[0])

'''
0     $16.678,36
1      $1.230,40
2          $1,02
3        $315,46
4          $1,00
5          $1,00
6        $0,4067
7        $0,1052
8        $0,3232
9        $0,8727
10         $5,48

'''

详细信息:

string = '$1,02$1,02-0,01%'
values = string.split('$') # output -- > ['', '1,02', '1,02-0,01%']
final_value = values[1] # we need only price. Thats why i choose the second element and apply this to all dataframe.

相关问题