我正在寻找一些帮助来更新我的 python 脚本以“匹配”熊猫而不是创建新列......我已经添加了下面的所有细节以及不正确和正确的结果。
任何帮助将非常感激。
test.csv
(原始 CSV 文件)
MATCH1,MATCH2,TITLE,TITLE,TITLE,TITLE,TITLE,TITLE,MATCH3,DATA,TITLE,TITLE
DMATCH1,MData (N/A),data,data,data,data,data,data,Tommy,55,data,data
DMATCH1,MData (N/A),data,data,data,data,data,data,Ben,54,data,data
DMATCH1,MData (N/A),data,data,data,data,data,data,Jim,52,data,data
DMATCH1,MData (N/A),data,data,data,data,data,data,Elz M,22,data,data
DMATCH2,MData (B/B),data,data,data,data,data,data,James Smith,15,data,data
DMATCH2,MData (B/B),data,data,data,data,data,data,Jessica Long,224,data,data
DMATCH2,MData (B/B),data,data,data,data,data,data,Mike,62,data,data
DMATCH3,Mdata,data,data,data,data,data,data,Joe Reane,66,data,data
DMATCH3,Mdata,data,data,data,data,data,data,Peter Jones,256,data,data
DMATCH3,Mdata,data,data,data,data,data,data,Lesley Lope,5226,data,data
test.txt
(原文文件)
MData (N/A)
DMATCH1
3 Tommy 144512/23332
1 Jim 90000/222311
1 Elz M 90000/222311
1 Ben 90000/222311
Data $50.90
misc2 $10.40
bla3 $20.20
MData (B/B)
DMATCH2
4 James Smith 2333/114441
4 Mike 90000/222311
4 Jessica Long 2333/114441
Data $50.90
bla3 $5.44
Test.py
(跑我)
import re
import pandas
# each block in the text file will be one element of this list
matchers = [[]]
i = 0
with open('test.txt') as infile:
for line in infile:
line = line.strip()
# Blocks are seperated by blank lines
if len(line) == 0:
i += 1
matchers.append([])
# assume there are always two blank lines between items
# and just skip to the lext line
infile.next()
continue
matchers[i].append(line)
# This regular expression matches the variable number of students in each block
studentlike = re.compile('(\d+) (.+) (\d+/\d+)')
# These are the names of the fields we expect at the end of each block
datanames = ['Data', 'misc2', 'bla3']
# We will build a table containing a list of elements for each student
table = []
for matcher in matchers:
# We use an iterator over the block lines to make indexing simpler
it = iter(matcher)
# The first two elements are match values
m1, m2 = it.next(), it.next()
# then there are a number of students
students = []
for possiblestudent in it:
m = studentlike.match(possiblestudent)
if m:
students.append(list(m.groups()))
else:
break
# After the students come the data elements, which we read into a dictionary
# We also add in the last possible student line as that didn't match the student re
dataitems = dict(item.split() for item in [possiblestudent] + list(it))
# Finally we construct the table
for student in students:
# We use the dictionary .get() method to return blanks for the missing fields
table.append([m1, m2] + student + [dataitems.get(d, '') for d in datanames])
csvdata = pandas.read_csv('test.csv')
textdata = pandas.DataFrame(table, columns=['MATCH2', 'MATCH1', 'TITLE01', 'MATCH3', 'TITLE02', 'Data', 'misc2', 'bla3'])
mergeddata = pandas.merge(csvdata, textdata, how='left', on=['MATCH1', 'MATCH2', 'MATCH3'], sort=False)
mergeddata.to_csv('test.csv', index=False)
test.csv
(运行后更新 CSV 文件test.py
)
MATCH1,MATCH2,TITLE,TITLE,TITLE,TITLE,TITLE,TITLE,MATCH3,DATA,TITLE,TITLE,,,,,
DMATCH1,MData (N/A),data,data,data,data,data,data,Tommy,55,data,data,3,144512/23332,Data $50.90,misc2 $10.40,bla3 $20.20
DMATCH1,MData (N/A),data,data,data,data,data,data,Ben,54,data,data,1,90000/222311,,,
DMATCH1,MData (N/A),data,data,data,data,data,data,Jim,52,data,data,1,90000/222311,,,
DMATCH1,MData (N/A),data,data,data,data,data,data,Elz M,22,data,data,1,90000/222311,,,
DMATCH2,MData (B/B),data,data,data,data,data,data,James Smith,15,data,data,4,2333/114441,Data $50.90,,bla3 $5.44
DMATCH2,MData (B/B),data,data,data,data,data,data,Jessica Long,224,data,data,4,2333/114441,,,
DMATCH2,MData (B/B),data,data,data,data,data,data,Mike,62,data,data,4,90000/222311,,,
DMATCH3,Mdata,data,data,data,data,data,data,Joe Reane,66,data,data,,,,,
DMATCH3,Mdata,data,data,data,data,data,data,Peter Jones,256,data,data,,,,,
DMATCH3,Mdata,data,data,data,data,data,data,Lesley Lope,5226,data,data,,,,,
test.txt
(更新的文本文件)
Mdata
DMATCH3
5 Joe Reane 0/0
5 Peter Jones 90000/222311
Data $10.91
misc2 $420.00
bla3 $210.00
由于文本文件已更新,我们需要重新运行test.py
,这将输出不正确/错误的以下内容:(test.csv
更新)
MATCH1,MATCH2,TITLE,TITLE.1,TITLE.2,TITLE.3,TITLE.4,TITLE.5,MATCH3,DATA,TITLE.6,TITLE.7,TITLE01_x,TITLE02_x,Data_x,misc2_x,bla3_x,TITLE01_y,TITLE02_y,Data_y,misc2_y,bla3_y
DMATCH1,MData (N/A),data,data,data,data,data,data,Tommy,55,data,data,3.0,144512/23332,$50.90,$10.40,$20.20,3,144512/23332,$50.90,$10.40,$20.20
DMATCH1,MData (N/A),data,data,data,data,data,data,Ben,54,data,data,1.0,90000/222311,$50.90,$10.40,$20.20,1,90000/222311,$50.90,$10.40,$20.20
DMATCH1,MData (N/A),data,data,data,data,data,data,Jim,52,data,data,1.0,90000/222311,$50.90,$10.40,$20.20,1,90000/222311,$50.90,$10.40,$20.20
DMATCH1,MData (N/A),data,data,data,data,data,data,Elz M,22,data,data,1.0,90000/222311,$50.90,$10.40,$20.20,1,90000/222311,$50.90,$10.40,$20.20
DMATCH2,MData (B/B),data,data,data,data,data,data,James Smith,15,data,data,4.0,2333/114441,$50.90,,$5.44,4,2333/114441,$50.90,,$5.44
DMATCH2,MData (B/B),data,data,data,data,data,data,Jessica Long,224,data,data,4.0,2333/114441,$50.90,,$5.44,4,2333/114441,$50.90,,$5.44
DMATCH2,MData (B/B),data,data,data,data,data,data,Mike,62,data,data,4.0,90000/222311,$50.90,,$5.44,4,90000/222311,$50.90,,$5.44
DMATCH3,Mdata,data,data,data,data,data,data,Joe Reane,66,data,data,,,,,,,,,,
DMATCH3,Mdata,data,data,data,data,data,data,Peter Jones,256,data,data,,,,,,,,,,
DMATCH3,Mdata,data,data,data,data,data,data,Lesley Lope,5226,data,data,,,,,,,,,,
正确的输出应该是一个更新的文件:test.csv
MATCH1,MATCH2,TITLE,TITLE,TITLE,TITLE,TITLE,TITLE,MATCH3,DATA,TITLE,TITLE,,,,,
DMATCH1,MData (N/A),data,data,data,data,data,data,Tommy,55,data,data,3,144512/23332,Data $50.90,misc2 $10.40,bla3 $20.20
DMATCH1,MData (N/A),data,data,data,data,data,data,Ben,54,data,data,1,90000/222311,,,
DMATCH1,MData (N/A),data,data,data,data,data,data,Jim,52,data,data,1,90000/222311,,,
DMATCH1,MData (N/A),data,data,data,data,data,data,Elz M,22,data,data,1,90000/222311,,,
DMATCH2,MData (B/B),data,data,data,data,data,data,James Smith,15,data,data,4,2333/114441,Data $50.90,,bla3 $5.44
DMATCH2,MData (B/B),data,data,data,data,data,data,Jessica Long,224,data,data,4,2333/114441,,,
DMATCH2,MData (B/B),data,data,data,data,data,data,Mike,62,data,data,4,90000/222311,,,
DMATCH3,Mdata,data,data,data,data,data,data,Joe Reane,66,data,data,5,0/0,,misc2 $420.00,bla3 $210.00
DMATCH3,Mdata,data,data,data,data,data,data,Peter Jones,256,data,data,5,90000/222311,,,
DMATCH3,Mdata,data,data,data,data,data,data,Lesley Lope,5226,data,data,,,,,
提前致谢
- Hyflex
jsexauer 的回溯错误
Traceback (most recent call last):
File "C:\test.py", line 62, in <module>
mergeddata = pandas.merge(csvdata, textdata, how='right', on=mergecols, sort=False)
File "C:\Python27\lib\site-packages\pandas\tools\merge.py", line 37, in merge
return op.get_result()
File "C:\Python27\lib\site-packages\pandas\tools\merge.py", line 197, in get_result
self._maybe_add_join_keys(result, left_indexer, right_indexer)
File "C:\Python27\lib\site-packages\pandas\tools\merge.py", line 222, in _maybe_add_join_keys
right_na_indexer))
ValueError: could not convert string to float: