0

新しい列を作成するのではなく、python スクリプトを pandas と「一致」するように更新するための助けを探しています...以下のすべての詳細と、正しくない結果と正しい結果を追加しました。

どんな助けでも大歓迎です。

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:
4

1 に答える 1