1

Please this is my code. I want to remove key from displaying in the output, just only the "values" and more also, since I group by the "date",I don't want the column to display in the list. Thanks

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)

Current output:

{
    "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'}"
    ]
}

Desired output:

{
    "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'}"
    ]
}
Stefan
  • 41,759
  • 13
  • 76
  • 81
Payne
  • 543
  • 2
  • 12
  • 32

1 Answers1

2

Try:

for date_range, flights in df.groupby('date'):
    flights_no_date = flights.drop('date', axis=1)
    json_dict[date_range]= map(list, flights_no_date.values)
Stefan
  • 41,759
  • 13
  • 76
  • 81
  • Thanks sir, please how can I remove date from the list since I have it already [date_range], I don't want it in the flights. Thanks – Payne Feb 01 '16 at 19:35
  • You could `df.drop('date', axis=1)` before `.groupby()` as it seems you don't need that `column` at all. – Stefan Feb 01 '16 at 19:39
  • I only need it to group, and display at the it is the desired output but not in the body – Payne Feb 01 '16 at 19:44
  • This is the error it brought: See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-cop – Payne Feb 01 '16 at 20:00
  • That's just a warning, you can try without the `inplace=True`, see updated. – Stefan Feb 01 '16 at 20:08
  • I don't think the `SettingWithCopyWarning` is of concern for you as you are not trying to modify the original `DataFrame` - more detail here - see last answer down the page: http://stackoverflow.com/questions/20625582/how-to-deal-with-this-pandas-warning – Stefan Feb 01 '16 at 20:34
  • Thanks sir! I have followed your update. It works perfectly now. Thanks sir – Payne Feb 01 '16 at 21:18