Sie NaN
ersetzen list
zu leeren, was nicht einfach ist - mask
oder combine_first
verwenden, indem Sie Series
durch leere lists
erstellt:
pos_tweets = [('I love this car', 'positive'),
('This view is amazing', 'positive'),
('I feel great this morning', 'positive'),
('I am so excited about the concert', 'positive'),
(None, 'positive')]
df = pd.DataFrame(pos_tweets, columns= ["Text","col2"])
print (df)
Text col2
0 I love this car positive
1 This view is amazing positive
2 I feel great this morning positive
3 I am so excited about the concert positive
4 None positive
stopwords = ['love','car','amazing']
s = pd.Series([[]], index=df.index)
df["Text"] = df["Text"].str.lower().str.split().mask(df["Text"].isnull(), s)
print (df)
Text col2
0 [i, love, this, car] positive
1 [this, view, is, amazing] positive
2 [i, feel, great, this, morning] positive
3 [i, am, so, excited, about, the, concert] positive
4 [] positive
df['Text']=df['Text'].apply(lambda x:' '.join([item for item in x if item not in stopwords]))
print (df)
Text col2
0 i this positive
1 this view is positive
2 i feel great this morning positive
3 i am so excited about the concert positive
4 positive
Eine andere Lösung:
stopwords = ['love','car','amazing']
df["Text"]=df["Text"].str.lower().str.split().combine_first(pd.Series([[]], index=df.index))
print (df)
Text col2
0 [i, love, this, car] positive
1 [this, view, is, amazing] positive
2 [i, feel, great, this, morning] positive
3 [i, am, so, excited, about, the, concert] positive
4 [] positive
Was meinen Sie mit "leere Reihe"? NaN, leere Saite? Und was ist die erwartete Ausgabe in diesem Fall? – FLab
Bitte geben Sie ein Beispiel an, das andere Benutzer ausprobieren können. – mhoff
Was machen Sie danach mit diesem bereinigten Text? Vielleicht sollten Sie die CountVectorized/TfidfVectorizer-Methoden überprüfen - sie können das "on the fly" machen ... – MaxU