从数据帧中挑选特定值?

2024-10-02 20:33:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从json url创建的数据帧。它有两列,bpi和time。bpi值是前三行的字典,时间值是NaN。最后三行是bpi的NaN和time的timedate值。我想返回第3行、第1列和第4行、第2列。复杂的部分是在第3行第1列中,我只想从字典中选择特定的键、值对。我该怎么做?你知道吗

[in] print(btc)
[out]
                                                          bpi  \
EUR         {'code': 'EUR', 'symbol': '€', 'rate': '3...   
GBP         {'code': 'GBP', 'symbol': '£', 'rate': '...   
USD         {'code': 'USD', 'symbol': '$', 'rate': '4,...   
updated                                                   NaN   
updatedISO                                                NaN   
updateduk                                                 NaN   

                                 time  
EUR                               NaN  
GBP                               NaN  
USD                               NaN  
updated     Aug 27, 2017 14:07:00 UTC  
updatedISO  2017-08-27T14:07:00+00:00  
updateduk   Aug 27, 2017 at 15:07 BST  

我只想要美元字典中的rate,加上time列中的updated值。你知道吗

每个日期值的输出应该是一行,后跟一列,列出rates

下面是使用pandas.to_dict修改df时的输出

[in]
btc_dict = btc.to_dict()

print(btc_dict)
[out]
{'bpi': {'EUR': {'code': 'EUR', 'symbol': '€', 'rate': '3,671.8281', 'description': 'Euro', 'rate_float': 3671.8281}, 'GBP': {'code': 'GBP', 'symbol': '£', 'rate': '3,397.8616', 'description': 'British Pound Sterling', 'rate_float': 3397.8616}, 'USD': {'code': 'USD', 'symbol': '$', 'rate': '4,378.8400', 'description': 'United States Dollar', 'rate_float': 4378.84}, 'updated': nan, 'updatedISO': nan, 'updateduk': nan}, 'time': {'EUR': nan, 'GBP': nan, 'USD': nan, 'updated': 'Aug 27, 2017 14:07:00 UTC', 'updatedISO': '2017-08-27T14:07:00+00:00', 'updateduk': 'Aug 27, 2017 at 15:07 BST'}}

Tags: ratetimecodenaneursymbolaugusd
2条回答

在我看来,最好是从dict创建新列:

d = {'bpi': {'EUR': {'code': 'EUR', 'symbol': '€', 'rate': '3,671.8281', 'description': 'Euro', 'rate_float': 3671.8281}, 'GBP': {'code': 'GBP', 'symbol': '£', 'rate': '3,397.8616', 'description': 'British Pound Sterling', 'rate_float': 3397.8616}, 'USD': {'code': 'USD', 'symbol': '$', 'rate': '4,378.8400', 'description': 'United States Dollar', 'rate_float': 4378.84}, 'updated': np.nan, 'updatedISO': np.nan, 'updateduk': np.nan}, 'time': {'EUR': np.nan, 'GBP': np.nan, 'USD': np.nan, 'updated': 'Aug 27, 2017 14:07:00 UTC', 'updatedISO': '2017-08-27T14:07:00+00:00', 'updateduk': 'Aug 27, 2017 at 15:07 BST'}}

df = pd.DataFrame(d)
#replace NaNs to {}
df['bpi'] = df['bpi'].fillna(pd.Series([{}], index=df.index))

#new df by constructor, join column time last
df1 = pd.DataFrame(df['bpi'].values.tolist(), index=df.index).join(df['time'])
#convert rate column to float 
df1['rate'] = df1['rate'].replace(',','',regex=True).astype(float)
#convert time column to datetimes
df1['time'] = pd.to_datetime(df1['time'])
print (df1)
           code             description       rate  rate_float   symbol  \
EUR         EUR                    Euro  3671.8281   3671.8281   €   
GBP         GBP  British Pound Sterling  3397.8616   3397.8616  £   
USD         USD    United States Dollar  4378.8400   4378.8400    $   
updated     NaN                     NaN        NaN         NaN      NaN   
updatedISO  NaN                     NaN        NaN         NaN      NaN   
updateduk   NaN                     NaN        NaN         NaN      NaN   

                          time  
EUR                        NaT  
GBP                        NaT  
USD                        NaT  
updated    2017-08-27 14:07:00  
updatedISO 2017-08-27 14:07:00  
updateduk  2017-08-27 15:07:00  

或者使用^{}solution与原始json数据类似(如果可能):

df = json_normalize(d)
print (df)

上次按^{}筛选:

#sample
df3 = df1[(df1['code'] == 'EUR') & (df1['rate'] > 1000)]
print (df3)
    code description       rate  rate_float  symbol time
EUR  EUR        Euro  3671.8281   3671.8281  €  NaT

IIUC,您可以使用df.loc访问这些值,如下所示:

r = df.loc['USD', 'bpi']['rate']
y = df.loc['updated', 'time']

df = pd.DataFrame({'btc_price (USD)': [r], 'time' : [y]}) 
print(df)

  btc_price (USD)                       time
0      4,378.8400  Aug 27, 2017 14:07:00 UTC

要保存到CSV,可以使用df.to_csv

df.to_csv('out.csv')

如果以后必须附加到同一个数据帧,则应首先创建一个新的数据帧并附加到现有的CSV文件:

df_new = pd.DataFrame([[new_rate, new_time]], columns=['btc_price (USD)', 'time'])
with open('out.csv', 'a') as f:
    df_new.to_csv(f, header=False)

相关问题 更多 >