【发布时间】:2017-12-10 22:30:11
【问题描述】:
我正在尝试对我的系列对象“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
【问题讨论】:
-
因为您正在变量中获取推文,对其进行一些更改,然后下一次迭代开始。您没有将更改的数据分配回系列。
标签: python pandas dataframe series