当我在Dataframe上面运行时,它显示一个错误字符串,索引必须是整数。我不知道如何解决这个问题
这是我到目前为止试过的代码
# Dictionary of English Contractions
contractions_dict = {"ain't": "are not","'s":" is","aren't": "are not",
"can't": "cannot","can't've": "cannot have",
"'cause": "because","could've": "could have","couldn't": "could not",
"couldn't've": "could not have", "didn't": "did not","doesn't": "does not",
"don't": "do not","hadn't": "had not","hadn't've": "had not have",
"hasn't": "has not","haven't": "have not","he'd": "he would",
"he'd've": "he would have","he'll": "he will", "he'll've": "he will have",
"how'd": "how did","how'd'y": "how do you","how'll": "how will",
"I'd": "I would", "I'd've": "I would have","I'll": "I will",
"I'll've": "I will have","I'm": "I am","I've": "I have", "isn't": "is not",
"it'd": "it would","it'd've": "it would have","it'll": "it will",
"it'll've": "it will have", "let's": "let us","ma'am": "madam",
"mayn't": "may not","might've": "might have","mightn't": "might not",
"mightn't've": "might not have","must've": "must have","mustn't": "must not",
"mustn't've": "must not have", "needn't": "need not",
"needn't've": "need not have","o'clock": "of the clock","oughtn't": "ought not",
"oughtn't've": "ought not have","shan't": "shall not","sha'n't": "shall not",
"shan't've": "shall not have","she'd": "she would","she'd've": "she would have",
"she'll": "she will", "she'll've": "she will have","should've": "should have",
"shouldn't": "should not", "shouldn't've": "should not have","so've": "so have",
"that'd": "that would","that'd've": "that would have", "there'd": "there would",
"there'd've": "there would have", "they'd": "they would"}
# Regular expression for finding contractions
contractions_re=re.compile('(%s)' % '|'.join(contractions_dict.keys()))
# Function for expanding contractions
def expand_contractions(text,contractions_dict=contractions_dict):
def replace(match):
return contractions_dict[match.group(0)]
# Expanding Contractions in the reviews
dataset['entitas bernama']=dataset['entitas bernama'].apply(lambda x:expand_contractions(x))
这就是错误所在
dataset['entitas bernama']=dataset['entitas bernama'].apply(lambda x:expand_contractions(x))
error : string indices must be integers
这就是如何替换pandas中的系列值
从https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.replace.html:
范例
为
Series.replace
使用参数inplace=True
以避免重新分配给df,即twt.tweets = twt.tweets.replace(...)
相关问题 更多 >
编程相关推荐