I'm actually trying to transform records of column of payment receipt into pandas dataframe. I read the records row by row and determine which data should be in which column. So I created empty dataframe like this:
df=pd.DataFrame
df['QTY']=None
df['Unit Price']=None
and try to fill that empty df like this: In this case, the record is "1X527,000 "
i=0
buff=[]
for line in df1.iterrows():
if 'X' in line:
try:
buff=[float(a.replace(',','').strip()) for a in line]
df.at[i,'QTY']=buff[0]
df.at[i,'Unit Price']= buff[1]
except ValueError:
break;
i+=1
I knew it would't work because there's yet no index in the data frame, but I don't know how to make dynamic data frame. Can you help me?
EDIT: This is just sample of the records. I opened using "open" in python because it has no regularity. I put it into other single-column data frame, df1.
7 1X 527,000
8 AUS MOL CRLD CD750 527,000 *
9 SUBTOTAL: 527,000
EDIT2: This is the working code:
for line in df1.itertuples():
if 'X' in line[1]:
try:
buff=[float(a) for a in line[1].strip().replace(',','').split('X',1)]
df_dict['QTY'].append(buff[0])
df_dict['Unit Price'].append(buff[1])
Thanks all!