2021年5月5日星期三

How to make my pandas script export to csv

I have managed to get the output I want from this script but I am having trouble exporting it to a csv using:

v.to_csv(n + '.csv', index=False)  

I get this error:

Traceback (most recent call last): Python/CouponRedemptions/start.py", line 22, in <module> print(v['invoice_line_normal_price']) File "~/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 2902, in getitem indexer = self.columns.get_loc(key) File "~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2893, in get_loc raise KeyError(key) from err KeyError: 'invoice_line_normal_price'  

I think it is the way the DF is structured, you cannot export it in its current state. I was wondering how I would go about making this work or any suggestions on where I cant start looking.

import pandas as pd  import re    r = pd.read_csv('cp.csv', low_memory=False)    r = r.filter(['shop_name','order_coupon_code','invoice_line_type','invoice_date','invoice_line_normal_price'])  r = r[r.order_coupon_code.notnull()]  r['invoice_line_normal_price'] = pd.to_numeric(r['invoice_line_normal_price'],errors = 'coerce')    n = input("Enter the coupon name: ")  nr = r[r.order_coupon_code.str.match(n,flags=re.IGNORECASE)]  nr = nr[nr.invoice_line_type.str.match('charge')]  nr = nr.sort_values('shop_name')  v = nr.groupby(['shop_name'])['invoice_line_normal_price'].value_counts().to_frame('counts')  print(v)  
https://stackoverflow.com/questions/67412147/how-to-make-my-pandas-script-export-to-csv May 06, 2021 at 01:08PM

没有评论:

发表评论