我有一个从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
值。你知道吗
每个日期值的输出应该是一行,后跟一列,列出rate
s
下面是使用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'}}
在我看来,最好是从
dict
创建新列:或者使用^{} 如solution与原始json数据类似(如果可能):
上次按^{} 筛选:
IIUC,您可以使用
df.loc
访问这些值,如下所示:要保存到CSV,可以使用
df.to_csv
:如果以后必须附加到同一个数据帧,则应首先创建一个新的数据帧并附加到现有的CSV文件:
相关问题 更多 >
编程相关推荐