2016-11-16 2 views
1

Meine Datenabtastwert ist:Wie man spezifische Interpunktion durch neuen Namen ersetzt?

 comment sarc_majority 
0  [?, ?]   sarc 
1   [0]  non-sarc 
2  [!, !, !]   sarc 
3   [0]  non-sarc 
4   [?]   sarc 

ich die Zeichensetzung mit einem neuen Namen ersetzt werden soll. Sowie ? = punct1,! = punct2, '= punct3. Ich habe versucht, aus der CSV-Datei lesen.

replace_df = pd.read_csv('./final/eng-mly-punct.csv', sep=',', quoting=csv.QUOTE_NONE, 
         names=["punct", "replacer"]) 
replace_df.head() 

    punct replacer 
0 ?  punct1 
1 !  punct2 
2 '  punct3 

Dann stucked ich zu ersetzen:

for punct, replacer in replace_df.itertuples(index=False,name=None): 
    df.comment = df.comment.str.replace(r'\b{0}\b'.format(punct),replacer) 

Der Fehler ist: Fehler: nichts

zu wiederholen, was falsch gelaufen? Oder gibt es einen möglichen Weg, dies zu tun? Die gewünschte Ausgabe sein sollte einfach:

     comment sarc_majority 
0    [punct1, punct1]   sarc 
1       [0]  non-sarc 
2  [punct2, punct2, punct2]   sarc 
3       [0]  non-sarc 
4      [punct1]   sarc 

Vielen Dank im Voraus. Prost.

+0

versuchen Sie es mit dem 're' Paket. 're (r '\?', punct1, text)' –

+0

Dank @RohanAmrute für Ihre Antwort. Das ist es, was ich zuerst denke. Aber lass uns den str.replace ausprobieren. –

Antwort

1

können Sie verwenden replace von dict d - muss aber entkommen ?-\?:

df = pd.DataFrame({'comment': {0: '[?, ?]', 1: '[0]', 2: '[!, !, !]', 3: '[0]', 4: '[?]'}, 'sarc_majority': {0: 'sarc', 1: 'non-sarc', 2: 'sarc', 3: 'non-sarc', 4: 'sarc'}}) 
print (df) 
    comment sarc_majority 
0  [?, ?]   sarc 
1  [0]  non-sarc 
2 [!, !, !]   sarc 
3  [0]  non-sarc 
4  [?]   sarc 

replace_df = pd.DataFrame({'replacer': {0: 'punct1', 1: 'punct2', 2: 'punct3'}, 'punct': {0: '?', 1: '!', 2: "'"}}) 
print (replace_df) 
    punct replacer 
0  ? punct1 
1  ! punct2 
2  ' punct3 
replace_df.punct = '\\' + replace_df.punct 
d = replace_df.set_index('punct')['replacer'].to_dict() 
print (d) 
{'\\!': 'punct2', "\\'": 'punct3', '\\?': 'punct1'} 

df.comment = df.comment.replace(d, regex=True) 
print (df) 
        comment sarc_majority 
0   [punct1, punct1]   sarc 
1      [0]  non-sarc 
2 [punct2, punct2, punct2]   sarc 
3      [0]  non-sarc 
4     [punct1]   sarc 

EDIT von Kommentar:

d = {'\?':'punct1','!':'punct2',"'":'punct3'} 
df.comment = df.comment.replace(d, regex=True) 
print (df) 
        comment sarc_majority 
0   [punct1, punct1]   sarc 
1      [0]  non-sarc 
2 [punct2, punct2, punct2]   sarc 
3      [0]  non-sarc 
4     [punct1]   sarc 

Sie können auch d von replace_df erstellen:

df = pd.DataFrame({'comment':[['?', '?'],[0], ['!', '!', '!'], [0], ['?']], 'sarc_majority': [ 'sarc','non-sarc', 'sarc', 'non-sarc','sarc']}) 
print (df) 
    comment sarc_majority 
0  [?, ?]   sarc 
1  [0]  non-sarc 
2 [!, !, !]   sarc 
3  [0]  non-sarc 
4  [?]   sarc 

print (type(df.ix[0,'comment'])) 
<class 'list'> 

replace_df = pd.DataFrame({'replacer': {0: 'punct1', 1: 'punct2', 2: 'punct3'}, 'punct': {0: '?', 1: '!', 2: "'"}}) 
#print (replace_df) 

replace_df.punct = '\\' + replace_df.punct.apply(lambda x: x.format()) 
d = replace_df.set_index('punct')['replacer'].to_dict() 
print (d) 
{'\\!': 'punct2', "\\'": 'punct3', '\\?': 'punct1'} 

df.comment = df.comment.apply(lambda x: pd.Series(x).astype(str).replace(d, regex=True).tolist()) 
print (df) 
        comment sarc_majority 
0   [punct1, punct1]   sarc 
1      [0]  non-sarc 
2 [punct2, punct2, punct2]   sarc 
3      [0]  non-sarc 
4     [punct1]   sarc 
+0

Was ist deine Pandas Version? – jezrael

+0

Oder vielleicht hast du eine Liste in der Kommentarspalte, welche return 'print (type (df.ix [0, 'comment']))'? – jezrael

+0

Danke @jezrael für deine Hilfe. Ich habe es versucht, aber es gibt immer noch die gleichen Interpunktionssymbole, keines wurde ersetzt :-(Ich lese den Post über re für regex [hier] (http://stackoverflow.com/questions/16720541/python-string- replace-regular-expression) Ist das jetzt mit mir verwandt? –

1

Die meisten Interpunktionszeichen haben in regulären Ausdrücken eine besondere Bedeutung. Hier haben Sie zB: \b?\b, was eine optionale Grenze gefolgt von einer Grenze bedeutet. Nicht was du meintest.

Für beliebige Zeichenketten in einem regexp vorbei, muss es re.escape entkommen werden:

import re 
r'\b{0}\b'.format(re.escape(punct)) 

Diese \b\?\b sein wird, die eine Grenze bedeutet, gefolgt von einer ?, durch eine andere Grenze gefolgt.

+1

'\ b' ist kein Leerzeichen, es ist eine Grenze zwischen Wortzeichen und Nichtwortzeichen. – cco

+0

@cco> du hast recht, was habe ich mir dabei gedacht. Bearbeitung. – spectras

+0

Danke @spectras. Ich habe versucht und gibt AttributeError zurück: Kann .str Accessor nur mit String-Werten verwenden, die np.object_dtype in Pandas verwenden –

Verwandte Themen