如何从dict和特定列中删除键?

2024-10-02 08:14:20 发布

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

这是我的密码。我想从输出中删除key,只显示“values”等等,因为我按“date”分组,所以我不想在列表中显示列。 谢谢

df = pd.read_csv('testdata.csv')
json_dict = {}
for date_range, flights in df.groupby('date'):
    json_dict[date_range]= [str(v) for v in flights.to_dict(orient='records')]

with open('testdata.json', 'w') as f:
     json.dump(json_dict, f, indent=4, sort_keys=True)

电流输出:

{
    "2001-04-10--2001-02-02": [
        "{'Date': '2001-04-10--2001-02-02', 'DptCity_DptCountry': 'Beijing_Japan', 'AuthorID': 'GGG', 'ArCity_ArCountry': 'Paris_France'}"
    ], 
    "2008-03-10--2001-02-02": [
        "{'Date': '2008-03-10--2001-02-02', 'DptCity_DptCountry': 'London_UK', 'AuthorID': 'BBB', 'ArCity_ArCountry': 'Lagos_Nigeria'}", 
        "{'Date': '2008-03-10--2001-02-02', 'DptCity_DptCountry': 'NewYork_US', 'AuthorID': 'DDD', 'ArCity_ArCountry': 'Nairobi_Kenya'}", 
        "{'Date': '2008-03-10--2001-02-02', 'DptCity_DptCountry': 'London_UK', 'AuthorID': 'EEE', 'ArCity_ArCountry': 'Paris_France'}", 
        "{'Date': '2008-03-10--2001-02-02', 'DptCity_DptCountry': 'NewYork_US', 'AuthorID': 'FFF', 'ArCity_ArCountry': 'Paris_France'}"
    ], 
    "2008-03-10--2001-04-02": [
        "{'Date': '2008-03-10--2001-04-02', 'DptCity_DptCountry': 'Beijing_Japan', 'AuthorID': 'CCC', 'ArCity_ArCountry': 'Paris_France'}"
    ], 
    "2008-03-12--2001-02-02": [
        "{'Date': '2008-03-12--2001-02-02', 'DptCity_DptCountry': 'NewYork_US', 'AuthorID': 'AAA', 'ArCity_ArCountry': 'Paris_France'}"
    ]
}

期望输出:

{
    "2001-04-10--2001-02-02": [
        "{'Beijing_Japan','GGG',  'Paris_France'}"
    ], 
    "2008-03-10--2001-02-02": [
        "{'London_UK', 'BBB','Lagos_Nigeria'}", 
        "{'NewYork_US','DDD','Nairobi_Kenya'}", 
        "{'London_UK','EEE','Paris_France'}", 
        "{'NewYork_US','FFF', 'ArCity_ArCountry': 'Paris_France'}"
    ], 
    "2008-03-10--2001-04-02": [
        "{'Beijing_Japan','CCC','Paris_France'}"
    ], 
    "2008-03-12--2001-02-02": [
        "{'NewYork_US','AAA','Paris_France'}"
    ]
}

Tags: jsondatedictusparisfrancenewyorkjapan

热门问题