I have extracted a column from a dataframe by searching for keywords in the name of column into a list given below:
last_element = [total amount 2,200.00
Name: 1, dtype: object,
0
net amount 2,200.00
vat amount 0.00
total amount 2,200.00
Name: 1, dtype: object,
0
total amount 2,200.00
Name: 1, dtype: object,
0
net amount 2,200.00
vat amount 0.00
total amount 2,200.00
Name: 1, dtype: object,
0
total Total Amount (AED)
Name: 4, dtype: object]
I wish to get the number 2,200
extracted from it but I haven't been able to do this.
I tried extracting it using regex:
for i in l:
re = re.findall(r'd+[,|.]?d+?[,|.]?d+?',i)
But, it throws an error
TypeError: expected string or bytes-like object
The following code shows that a list of dataframe was taken and then, keywords amount, balance and total
were searched in the column name and after that the last value of the column was to be extracted in last_element
.
for dataframe in lists_dataframe:
dataframe.columns= dataframe.columns.str.strip().str.lower()
keyword = ['total', 'amount', 'balance']
for key in keyword:
df = dataframe.filter(like = key)
if df.empty != True:
amount_column.append(df)
for s in amount_column:
last_element.append(s.iloc[-1])
for i in last_element:
res = re.findall(r'd+[,|.]?d+?[,|.]?d+?',i)
print(res)
From the last_element
, I wish to extract numbers like 2,200.00
Thanks in advance!
question from:
https://stackoverflow.com/questions/65903734/extract-numbers-from-part-of-a-list-which-is-a-part-of-a-dataframe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…