如何替换数据框python中的所有单词

时间:2019-03-19 00:20:37

标签: python python-3.x dataframe

我正在尝试将AGL票据转换为数据框,以便将所需的值放入Excel电子表格中。

我被困在尝试.replace()行中没有任何字符的字符,只剩下数字(尝试删除数据框中的所有单词)。另一个问题是在每个单元格中都有多个单词和数字。

Here is the current database:

from tabula import read_pdf
import openpyxl
from openpyxl import load_workbook
import pandas as pd
import numpy as np

df1 = tabula.read_pdf('C:/Users/Blake/Desktop/Python/AGL_Bill.pdf',guess=False, pages=2)
df1.columns = ['Description', 'Blank', 'Values']




df1.drop(labels=None, axis=None, index=[0,1,3,4,7,8,25,26,19,15,16,20,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62], columns=None, level=None, inplace=True, errors='raise')
df1.drop(labels=None, axis=1, columns=['Values'], level=None, inplace=True, errors='raise')





df1['Description'].str.replace('kWh', '')



print (df1)

df1.to_csv('Tableone.csv', encoding='utf-8')


wb2 = load_workbook('C:/Users/Blake/Desktop/ETemplate.xlsx')


wb2.create_sheet('DATA')
wb2.save('C:/Users/Blake/Desktop/Template.xlsx')`

1 个答案:

答案 0 :(得分:-1)

如果您尝试不使用任何字符替换字符,则RegEx使用数字(每个单元格)将它们连接在一起。

重新导入

import pandas as pd

data={'1':'Some dumb data $200.22 for me','2':'Some more really dumb data $5.23'}
df=pd.DataFrame.from_dict(data,orient='index')
df.columns=['Data']

def Num_Only(val):
    return ' '.join(re.findall('[\d\.]+',val))

df['New']=''
df.New=df.Data.apply(lambda x: Num_Only(x))
Which should output a new Dataframe ... like this

现在输出为...我删除了$,因为它没有用。

1.   Some dumb data $200.22 for me  200.22
2   Some more really dumb data $5.23    5.23

希望能带你去