数据帧包含
TRANSACTION FILE
AS REPORT 2018-12-02
Jeff Thomoson 000 11-28-2018 Payments 2,400 Wired transfer
Jeff Thomoson 000 11-29-2018 Interest 100 account
Paul Simson 000 11-12-2018 Payments 1,000 Wired transfer
Paul Simson 000 11-18-2018 Payments 100 net banking
John Sans 000 11-28-2018 Payments 300 cheque
Total 3,900
代码:
import pandas as pd
bad_words = ['TRANSACTION','REPORT','Total']
with open('e:\\data\\test.txt') as oldfile, open('e:\\data\\processed.txt', 'w') as newfile:
for line in oldfile:
if not any(bad_word in line for bad_word in bad_words):
newfile.write(line)
df_a = pd.read_csv('e:\\data\\processed.txt',header=None)
names = ['USER','NAME', 'TR Mode', 'Date', 'Narration', 'Amt', 'Mode']
df=pd.read_fwf('e:\\data\\processed.txt', header=None, names=names,dtype=str)
我试图连接变量(LedgerCode)中的值。但它将所有列添加为'02'
LedgerCode='02'
尝试了f字符串表达式,但没有成功。你知道吗
f"{LedgerCode}"+ df[['USER','NAME', 'TR Mode', 'Date', 'Narration', 'Amt', 'Mode']]
预期结果如下:
02 Jeff Thomoson 000 11-28-2018 Payments 2,400 Wired transfer
02 Jeff Thomoson 000 11-29-2018 Interest 100 account
02 Paul Simson 000 11-12-2018 Payments 1,000 Wired transfer
02 Paul Simson 000 11-18-2018 Payments 100 net banking
02 John Sans 000 11-28-2018 Payments 300 cheque
只需在数据框中插入一个新的分类账列(就地):
我不知道你为什么坚持使用f-string表达式。你知道吗
希望上面的代码能解决您的问题。你知道吗
相关问题 更多 >
编程相关推荐