为什么函数只返回最后一个值?

时间:2019-10-10 10:59:35

标签: python-3.x

我的时间格式为:pandas._libs.tslibs.timestamps.Timestamp,我需要在此timeSeries中选择每一分钟

2011-08-01 00:00:14
2011-08-01 00:00:17
2011-08-01 00:00:20
2011-08-01 00:00:23
2011-08-01 00:00:26
2011-08-01 00:00:29
2011-08-01 00:00:32
2011-08-01 00:00:35
2011-08-01 00:00:38
2011-08-01 00:00:41
2011-08-01 00:00:44
2011-08-01 00:01:00
2011-08-01 00:01:03
2011-08-01 00:01:06
2011-08-01 00:01:09
2011-08-01 00:01:12
2011-08-01 00:02:03
2011-08-01 00:02:06
2011-08-01 00:01:15
...
...

所以下面是到目前为止我尝试过的代码:

def mins():

    for i in range(len(df1['Time'])):
        labels = df1['Time'][i].strftime('%M')

        print (labels)
    return (labels)

因此,如果我调用该函数,则仅将最后一分钟作为输出返回,它实际上显示了其余的分钟,但未作为“输出”显示,因此我以后无法调用它,因为我尝试了很多事情(即首先添加标签),但没有任何效果,请您帮助别人。

2 个答案:

答案 0 :(得分:0)

labels变量在循环的每次迭代中都会更新。解决此问题的一种方法是改为在每次迭代中附加该项:

labels = []
for i in range(len(df1['Time'])):
    labels.append(df1['Time'][i].strftime('%M'))
return labels

但这效率很低。您可以这样做:

list(df1['Time'].apply(lambda x: x.strftime('%M')))

这会将lambda函数(x.strftime('%M'))应用于系列的每个元素,甚至:

[x.strftime('%M') for x in list(df1['Time'])]

这首先将系列转换为列表,然后使用列表理解来应用该功能。

答案 1 :(得分:0)

Art为您的问题提供了简洁明了的单行解决方案。正如他指出的那样,“标签”是一个单一值,每次您循环“ for”循环时都会被覆盖。您需要定义一个列表,以便append()正常工作。如果您想让代码正常工作,它应该看起来像这样:

#Import the necessary modules
import datetime as dt
import pandas as pd

#Make a compatible data set to test (note I copied and pasted the first five values but then edited the minutes to make it more interesting)
time_series = ['2011-08-01 00:00:14', '2011-08-01 00:00:17', '2011-08-01 00:01:20', '2011-08-01 00:02:23', '2011-08-01 00:05:26']
dates_list = [dt.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') for date in time_series]
#Convert to a dataframe
df = pd.DataFrame(dates_list, columns=['Time'])

#define a function (sorry I didn't like mins for a function name)
def get_mins(df1): #pass the dataframe to the function
    list_of_mins = [] #Initialise an empty list
    for i in range(len(df1['Time'])): 
        labels = df1['Time'][i].strftime('%M')
        list_of_mins.append(labels) # append labels to the list
        print (labels) #Probably want to delete this line
    return (list_of_mins) #Return the list

get_mins(df)

这应该输出:

00

00

01

02

05

['00','00','01','02','05']

祝你好运!

相关问题