选定列上的DataFrame.values

时间:2019-07-25 03:37:21

标签: python pandas

当我尝试不获取所有值而是仅获取指定的列时出现以下错误。我认为错误来自我在.values之后指定的列 任何帮助,将不胜感激。

supp_bal数据框:

          circulating_supply  total_supply
currency                                  
0xBTC                4758600      20999984
1337             26456031141   29258384256
1SG                  2187147      22227000
1ST                 85558370      93468691
1WO                 20981450      37219452
1X2                        0       3051868
2GIVE              521605983     521605983
42                        41            41
611                   478519        478519
777                        0   10000000000
A                   26842657     278273649
AAA                 15090818     397000000

pos_bal数据框:

                      2019-07-23  2019-07-24
 app_vendor_id currency                        
 3             1WO           2604      2304
               ABX             44      44
               ADH            822      82
               ALX             25      200
               AMLT          3673      367
               BCH            -41      -26

我的代码:

f = pos_bal.index.get_level_values('currency')
supp_bal['circulating_supply'].loc[f].values['circulating_supply']

错误:

   pos_bal['circulating_supply'] = supp_bal['circulating_supply'].loc[f].values['circulating_supply']
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

2 个答案:

答案 0 :(得分:0)

您的意思是:

<?php

// Store JSON data in a PHP variable
$json = '{"data":[{"name":"John","sex":"M","address":{"unit":"7"}}]}';

var_dump(json_decode($json));
?>

答案 1 :(得分:0)

您不需要在values之后使用列名, 这应该工作:

import pandas as pd
import numpy as np
supp_bal = pd.read_csv('D:\\supp_bal.csv', header=0)
pos_bal = pd.read_csv('D:\\pos_bal.csv', header=0)
supp_bal = supp_bal.set_index('currency')
pos_bal = pos_bal.set_index(['app_vendor_id', 'currency'])
display(supp_bal)
display(pos_bal)
f = pos_bal.index.get_level_values('currency')
pos_bal['circulating_supply']= supp_bal['circulating_supply'].loc[f].values
display(pos_bal)

输出

          circulating_supply  total_supply
currency                                  
0xBTC                4758600      20999984
1337             26456031141   29258384256
1SG                  2187147      22227000
1ST                 85558370      93468691
1WO                 20981450      37219452
1X2                        0       3051868
2GIVE              521605983     521605983
42                        41            41
611                   478519        478519
777                        0   10000000000
A                   26842657     278273649
AAA                 15090818     397000000


                        7/23/2019  7/24/2019
app_vendor_id currency                      
3             1WO            2604       2304
              ABX              44         44
              ADH             822         82
              ALX              25        200
              AMLT           3673        367

最终pos_bal

                        7/23/2019  7/24/2019  circulating_supply
app_vendor_id currency                                          
3             1WO            2604       2304          20981450.0
              ABX              44         44                 NaN
              ADH             822         82                 NaN
              ALX              25        200                 NaN
              AMLT           3673        367                 NaN

请注意,在您提供的数据中,两个数据帧中仅出现1WO,这就是为什么其他行都是NaN的原因。

顺便说一句,我有熊猫0.24.2