【发布时间】:2018-07-05 23:05:57
【问题描述】:
我在以下 CSV 文件中有数据,可在此处找到:
http://s000.tinyupload.com/index.php?file_id=87473936848618674050
CSV 的屏幕截图:
我编写了以下代码,将 CSV 文件作为 Pandas 数据框导入 Python,然后创建字典 dict。字典必须有名称和区域作为键,Windows 和 Linux 价格作为字典值。
#Import libraries and CSV file into dataframe, renaming columns, printing head
import pandas as pd
df = pd.read_csv('file.csv')
col_names = ['Name','Region','API', 'Memory','vCPU', 'Storage', 'Linux', 'Windows' ]
df.columns = col_names
#Creating Dict
dict = {}
for i in df.index:
key = (df.at[i, 'Name'] , df.at[i, 'Region'])
value = (df.at[i, 'vCPU'], df.at[i, 'Memory'], df.at[i, 'Storage'], df.at[i, 'Windows'] , df.at[i, 'Linux'])
dictionary = {key:value}
dict.update(dictionary)
我现在想编写一个函数来搜索字典。
例如,用户将为 vCPU 输入“32”,该函数将为具有 32 个 vCPU 的任何处理器返回区域、名称以及 Linux 和 Windows 价格。
稍后,我想实现这个针对 vCPU、Memory 和 Storage 的搜索功能。 (完整的 CSV 有 1700 行)。非常感谢有人帮助我。
【问题讨论】:
标签: python pandas dictionary dataframe search