[英]Removing DataFrame rows by comparing dates
I have a dataset and only wants to have the rows inside a time range. 我有一个数据集,只想在一个时间范围内包含行。 I put all the good rows in a Series object. 我将所有好的行放入Series对象中。 But when I re-assign that object to the DataFrame object, I get NaT values: 但是,当我将该对象重新分配给DataFrame对象时,会得到NaT值:
code: 码:
def get_tweets_from_range_in_csv():
csvfile1 = "results_dataGOOGL050"
df1 = temp(csvfile1)
def temp(csvfile):
tweetdats = []
d = pd.read_csv(csvfile + ".csv", encoding='latin-1')
start = datetime.datetime.strptime("01-01-2018", "%d-%m-%Y")
end = datetime.datetime.strptime("01-06-2018", "%d-%m-%Y")
for index, current_tweet in d['Date'].iteritems():
date_tw = datetime.datetime.strptime(current_tweet[:10], "%Y-%m-%d")
if start <= date_tw <= end:
tweetdats.append(date_tw)
else:
d.drop(index, inplace=True)
d = d.drop("Likes", 1)
d = d.drop("RTs", 1)
d = d.drop("Sentiment", 1)
d = d.drop("User", 1)
d = d.drop("Followers", 1)
df1['Date'] = pd.Series(tweetdats)
return d
Output of tweetdats: tweetdats的输出:
tweetdats
Out[340]:
[datetime.datetime(2018, 1, 30, 0, 0),
datetime.datetime(2018, 4, 1, 0, 0),
datetime.datetime(2018, 4, 1, 0, 0),
datetime.datetime(2018, 4, 1, 0, 0),
datetime.datetime(2018, 1, 5, 0, 0),
datetime.datetime(2018, 1, 5, 0, 0),
datetime.datetime(2018, 1, 8, 0, 0),
datetime.datetime(2018, 1, 20, 0, 0),
datetime.datetime(2018, 1, 22, 0, 0),
datetime.datetime(2018, 1, 5, 0, 0)]
You do not need to iterate through your dataframe with a for
loop to select the rows inside the time range of interest. 您无需使用for
循环遍历数据框即可选择感兴趣的时间范围内的行。
Let us assume that your initial dataframe df
has a 'Date' column containing the dates in datetime format; 让我们假设您的初始数据框df
有一个“日期”列,其中包含日期时间格式的日期; you can then simply create a new dataframe new_df
: 然后,您可以简单地创建一个新的数据new_df
:
new_df=df[(pd.to_datetime(df.time) > start) & (pd.to_datetime(self.df.time) < end)]
This way you do not have to copy and paste the "good" rows in a Series and then reassign them to a dataframe. 这样,您不必复制和粘贴系列中的“好”行,然后将它们重新分配给数据框。
Your temp
function would look like: 您的temp
函数如下所示:
def temp(csvfile):
df = pd.read_csv(csvfile + ".csv", encoding='latin-1')
start = datetime.datetime.strptime("01-01-2018", "%d-%m-%Y")
end = datetime.datetime.strptime("01-06-2018", "%d-%m-%Y")
new_df=df[(pd.to_datetime(df.time) > start) & (pd.to_datetime(self.df.time) < end)]
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.