首页 > 解决方案 > 从作为数据框一部分的列表的一部分中提取数字

问题描述

我通过在下面给出的列表中搜索列名中的关键字来从数据框中提取一列:

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]

我希望从中2,200提取号码,但我无法做到这一点。

我尝试使用正则表达式提取它:

for i in l:
    re = re.findall(r'\d+[,|.]?\d+?[,|.]?\d+?',i)

但是,它会引发错误

TypeError: expected string or bytes-like object

下面的代码显示了一个数据框列表,然后amount, balance and total在列名中搜索关键字,然后在 中提取列的最后一个值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)

从 中last_element,我希望提取像2,200.00

提前致谢!

标签: pythonpython-3.xpandaslistdataframe

解决方案


推荐阅读