简体   繁体   中英

How do I format my pandas dataframe through python to get columns in the csv output?

I am writing code such that I get hours of operation of a restaurant:

This is my input code:

merge_HOO = my_list  
merge_HOO=pd.DataFrame([(merge_HOO)],
columns = ['day','start','end']) 
print(merge_HOO)
merge_HOO.to_csv('merge_HOO_test1.csv')

OUTPUT:

    hours_type  is_open_now open
0   REGULAR FALSE   [{'start': '1100', 'end': '2200', 'day': 0}, {'start': '1100', 'end': '2200', 'day': 1}, {'start': '1100', 'end': '2200', 'day': 2}, {'start': '1100', 'end': '2200', 'day': 3}, {'start': '1100', 'end': '2200', 'day': 4}, {'start': '1100', 'end': '2200', 'day': 5}, {'start': '1100', 'end': '2100', 'day': 6}]

However, I want the output to look like in Column 1: header should be 'day' , column 2: 'start' , column 3: 'end' and under each column should be the respective values.

I also got this error when I ran the code:

con=len(content)))

AssertionError: 3 columns passed, passed data had 1 columns

Edit: Added what my_list looks like.

my_list = [{'open': [{'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 0},
           {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 1},
           {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 2},
           {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 3},
           {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 4},
           {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 5},
           {'is_overnight': False, 'start': '1100', 'end': '2100', 'day': 6}], 'hours_type': 'REGULAR',
  'is_open_now': False}]

for i in my_list[0]["open"]:
    del i["is_overnight"]

print(my_list)

How do I do this?

You have adapt the structure of merge_HOO to a dict whose keys contain the data for your columns. Since I suppose you will have more than one restaurant in the end, I added a counter item for each restaurant in my_list

my_list = [{'open': [{'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 0},
                     {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 1},
                     {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 2},
                     {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 3},
                     {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 4},
                     {'is_overnight': False, 'start': '1100', 'end': '2200', 'day': 5},
                     {'is_overnight': False, 'start': '1100', 'end': '2100', 'day': 6}], 'hours_type': 'REGULAR',
            'is_open_now': False}]

merge_HOO = {'item': [], 'day': [], 'start': [], 'end': []}
for i, restaurant in enumerate(my_list):
    for item in restaurant['open']:
        merge_HOO['item'].append(i)
        merge_HOO['day'].append(item['day'])
        merge_HOO['start'].append(item['start'])
        merge_HOO['end'].append(item['end'])

merge_HOO = pd.DataFrame(merge_HOO,
                         columns=['item', 'day', 'start', 'end'])
print(merge_HOO)

It will yield

   item  day start   end
0     0    0  1100  2200
1     0    1  1100  2200
2     0    2  1100  2200
3     0    3  1100  2200
4     0    4  1100  2200
5     0    5  1100  2200
6     0    6  1100  2100

Is that what you wanted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM