我试图对我的Series对象'tweet_text'中的每个字符串进行更改,但由于某些原因,在对for循环中的推文进行更改后打印系列对象时,我得到的字符串与之前的for循环。我怎样才能解决这个问题?为什么当我改变它们时,我的熊猫数据框没有更新它的值?
import pandas as pd
import re
import string
df = pd.read_csv('sample-tweets.csv',
names=['Tweet_Date', 'User_ID', 'Tweet_Text', 'Favorites', 'Retweets', 'Tweet_ID'])
sum_df = df[['User_ID', 'Tweet_ID', 'Tweet_Text']].copy()
sum_df.set_index(['User_ID'])
# print sum_df
tweet_text = df.ix[:, 2]
print type(tweet_text)
# efficiency could be im proved by using translate method
# regex = re.compile('[%s]' % re.escape(string.punctuation))
for tweet in tweet_text:
tweet = re.sub('https://t.co/[a-zA-Z0-9]*', "", tweet)
tweet = re.sub('@[a-zA-Z0-9]*', '', tweet)
tweet = re.sub('#[a-zA-Z0-9]*', '', tweet)
tweet = re.sub('$[a-zA-Z0-9]*', '', tweet)
tweet = ''.join(i for i in tweet if not i.isdigit())
tweet = tweet.replace('"', '')
tweet = re.sub(r'[\(\[].*?[\)\]]', '', tweet) # takes out everything between parentheses also, fix this
# gets rid of all punctuation and emoji's
tweet = "".join(l for l in tweet if l not in string.punctuation)
tweet = re.sub(r'[^\x00-\x7F]+',' ', tweet)
# gets ride of all extra spacing
tweet = tweet.lower()
tweet = tweet.strip()
tweet = " ".join(tweet.split())
count = count + 1
# print tweet
print tweet_text
因为您正在变量中发布推文,请对其进行一些更改,然后再开始下一次迭代。您不会将已更改的数据重新分配给该系列。 – TrigonaMinima