我在Convert a Pandas DataFrame to a dictionary上找到了将数据框转换为字典的指南。但是,我似乎无法更改代码以将输出转换为字典。
以下是我的代码。
import pandas as pd
import collections
governmentcsv = pd.read_csv('government-procurement-via-gebiz.csv',parse_dates=True) #read csv and it contain dates (parse_dates = true)
extract = governmentcsv.loc[:, ['supplier_name','award_date']] #only getting these columns
extract.award_date= pd.to_datetime(extract.award_date)
def extract_supplier_not_na_2015():
notNAFifteen = extract[(extract.supplier_name != 'na') & (extract.award_date.dt.year == 2015)] #extract only year 2015
notNAFifteen.reset_index(drop = True,inplace=True) #reset index
notNAFifteen.index += 1 #and index start from 1
#SortednotNAFifteen = collections.orderedDictionary(notNAFifteen)
return notNAFifteen
print extract_supplier_not_na_2015()
输出为:
supplier_name award_date
1 RMA CONTRACTS PTE. LTD. 2015-01-28
2 TESCOM (SINGAPORE) SOFTWARE SYSTEMS TESTING PT... 2015-07-01
3 MKS GLOBAL PTE. LTD. 2015-04-24
4 CERTIS TECHNOLOGY (SINGAPORE) PTE. LTD. 2015-06-26
5 RHT COMPLIANCE SOLUTIONS PTE. LTD. 2015-08-14
6 CLEANMAGE PTE. LTD. 2015-07-30
7 SOLUTIONSATWORK PTE. LTD. 2015-11-23
8 Ufinity Pte Ltd 2015-05-04
9 NCS PTE. LTD. 2015-01-28
最佳答案
我认为我找到了这个数据集:
https://data.gov.sg/dataset/government-procurement
反正这是代码
import pandas as pd
df = pd.read_csv('government-procurement-via-gebiz.csv',
encoding='unicode_escape',
parse_dates=['award_date'],
infer_datetime_format=True,
usecols=['supplier_name', 'award_date'],
)
df = df[(df['supplier_name'] != 'Unknown') & (df['award_date'].dt.year == 2015)].reset_index(drop=True)
#Faster way:
d1 = df.set_index('supplier_name').to_dict()['award_date']
#Alernatively:
d2 = dict(zip(df['supplier_name'], df['award_date']))
关于python - 如何从 Pandas 数据框转换为字典,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58462142/