使用Python Pandas将列连接到DataFrame

时间:2018-12-20 08:25:15

标签: python pandas

数据框包含

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

2 个答案:

答案 0 :(得分:1)

只需在数据框中插入一个新的分类帐列(就地):

df.insert(loc=0, column='ledger_code', value='02')

答案 1 :(得分:0)

我不确定您为什么坚持使用f字符串表达式。

df['LedgerCode'] = LedgerCode

希望上面的代码可以解决您的问题。