在 Pandas 中的列中使用 apply() 函数时传递行号
Pass row numbers while using apply() function in a column in Pandas
基本上我要做的是格式化日期列。日期为:3 月 24 日、7 月 5 日等。我写了一个函数来拆分这些并使其像 24/03 和 05/07。但问题是我的 pandas 数据框中的第 0 到 8 行是 2021 年的,其余行是 2020 年的。所以基本上使用当前代码我可以将 24th Mar 设置为 24/03 但我想要如果行号在 0 到 8 之间,则为 24/03/2021;如果行号在 8 之后,则为 24/03/2021。
import operator
def dateConversion(date):
day =''
month = ''
val_month = 0
if operator.contains(date, "th"):
day, month = date.split("th")
if operator.contains(date, "rd"):
day, month = date.split("rd")
if operator.contains(date, "nd"):
day, month = date.split("nd")
if operator.contains(date, "st"):
day, month = date.split("st")
day = day.strip()
if(int(day) < 10):
day = str(day)
day = '0' + day
month = month.strip()
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] #list items based on data
if month in months:
val_month = months.index(month) + 1 #getting month value
if(val_month < 10):
val_month = str(val_month)
val_month = '0' + val_month
day = day + '/' + str(val_month) #+ '/' + year
return day
我使用以下代码应用于列:
df_ipo['Listed Date_'] = df_ipo['Listed Date'].apply(lambda x: dateConversion(x))
如何在 dateConversion 中使用此应用函数传递行号,以便我可以相应地设置年份。
我们可以 replace
缩写,而不是按行解析日期(这可能很慢),并将年份添加到前几行。然后,这使我们能够轻松地转换为 datetime
dtype,它能够使用 strftime
.
轻松地将日期格式化为您的字符串
为了清楚起见,我保留了所有 4 列(原始的,删除了缩写并添加了年份,datetime
和字符串格式的日期),但没有真正的理由创建所有这些单独的列。
示例数据
import pandas as pd
df = pd.DataFrame({'date': ['24th Mar', '5th Jul', '1st May', '2nd Jun', '3rd Jul',
'30th May', '21st Oct', '18th Dec', '5th Sep', '16th Dec']})
repl = {'1st': '1', '2nd': '2', '3rd': '3', '4th': '4', '5th': '5',
'6th': '6', '7th': '7', '8th': '8', '9th': '9', '0th': '0'}
df['date_strip'] = df['date'].replace(repl, regex=True)
# Add 2021 to first 8 rows, then 2020 to the rest
df['date_strip'] = df['date_strip'] + ' ' + (['2021']*8 + ['2020']*(len(df)-8))
df['datetime'] = pd.to_datetime(df.date_strip, format='%d %b %Y')
df['date_fmt'] = df['datetime'].dt.strftime('%d/%m/%y')
date date_strip datetime date_fmt
0 24th Mar 24 Mar 2021 2021-03-24 24/03/21
1 5th Jul 5 Jul 2021 2021-07-05 05/07/21
2 1st May 1 May 2021 2021-05-01 01/05/21
3 2nd Jun 2 Jun 2021 2021-06-02 02/06/21
4 3rd Jul 3 Jul 2021 2021-07-03 03/07/21
5 30th May 30 May 2021 2021-05-30 30/05/21
6 21st Oct 21 Oct 2021 2021-10-21 21/10/21
7 18th Dec 18 Dec 2021 2021-12-18 18/12/21
8 5th Sep 5 Sep 2020 2020-09-05 05/09/20
9 16th Dec 16 Dec 2020 2020-12-16 16/12/20
基本上我要做的是格式化日期列。日期为:3 月 24 日、7 月 5 日等。我写了一个函数来拆分这些并使其像 24/03 和 05/07。但问题是我的 pandas 数据框中的第 0 到 8 行是 2021 年的,其余行是 2020 年的。所以基本上使用当前代码我可以将 24th Mar 设置为 24/03 但我想要如果行号在 0 到 8 之间,则为 24/03/2021;如果行号在 8 之后,则为 24/03/2021。
import operator
def dateConversion(date):
day =''
month = ''
val_month = 0
if operator.contains(date, "th"):
day, month = date.split("th")
if operator.contains(date, "rd"):
day, month = date.split("rd")
if operator.contains(date, "nd"):
day, month = date.split("nd")
if operator.contains(date, "st"):
day, month = date.split("st")
day = day.strip()
if(int(day) < 10):
day = str(day)
day = '0' + day
month = month.strip()
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] #list items based on data
if month in months:
val_month = months.index(month) + 1 #getting month value
if(val_month < 10):
val_month = str(val_month)
val_month = '0' + val_month
day = day + '/' + str(val_month) #+ '/' + year
return day
我使用以下代码应用于列:
df_ipo['Listed Date_'] = df_ipo['Listed Date'].apply(lambda x: dateConversion(x))
如何在 dateConversion 中使用此应用函数传递行号,以便我可以相应地设置年份。
我们可以 replace
缩写,而不是按行解析日期(这可能很慢),并将年份添加到前几行。然后,这使我们能够轻松地转换为 datetime
dtype,它能够使用 strftime
.
为了清楚起见,我保留了所有 4 列(原始的,删除了缩写并添加了年份,datetime
和字符串格式的日期),但没有真正的理由创建所有这些单独的列。
示例数据
import pandas as pd
df = pd.DataFrame({'date': ['24th Mar', '5th Jul', '1st May', '2nd Jun', '3rd Jul',
'30th May', '21st Oct', '18th Dec', '5th Sep', '16th Dec']})
repl = {'1st': '1', '2nd': '2', '3rd': '3', '4th': '4', '5th': '5',
'6th': '6', '7th': '7', '8th': '8', '9th': '9', '0th': '0'}
df['date_strip'] = df['date'].replace(repl, regex=True)
# Add 2021 to first 8 rows, then 2020 to the rest
df['date_strip'] = df['date_strip'] + ' ' + (['2021']*8 + ['2020']*(len(df)-8))
df['datetime'] = pd.to_datetime(df.date_strip, format='%d %b %Y')
df['date_fmt'] = df['datetime'].dt.strftime('%d/%m/%y')
date date_strip datetime date_fmt
0 24th Mar 24 Mar 2021 2021-03-24 24/03/21
1 5th Jul 5 Jul 2021 2021-07-05 05/07/21
2 1st May 1 May 2021 2021-05-01 01/05/21
3 2nd Jun 2 Jun 2021 2021-06-02 02/06/21
4 3rd Jul 3 Jul 2021 2021-07-03 03/07/21
5 30th May 30 May 2021 2021-05-30 30/05/21
6 21st Oct 21 Oct 2021 2021-10-21 21/10/21
7 18th Dec 18 Dec 2021 2021-12-18 18/12/21
8 5th Sep 5 Sep 2020 2020-09-05 05/09/20
9 16th Dec 16 Dec 2020 2020-12-16 16/12/20