两个列表上的Python函数定义
我想将年和月列合并成年和月的格式(即替换原来的格式)。我怎么做呢?以下方法在Python中似乎不起作用。谢谢两个列表上的Python函数定义,python,pandas,Python,Pandas,我想将年和月列合并成年和月的格式(即替换原来的格式)。我怎么做呢?以下方法在Python中似乎不起作用。谢谢 Year Month Year_month 2009 2 2009/2 2009 3 2009/3 2007 4 2007/3 2006 10 2006/10 Year_month 200902 200903 200704 200610 我认为你可以使用: 每列中的值是什么类型的?字符串,整数?同样,这是熊
Year Month Year_month
2009 2 2009/2
2009 3 2009/3
2007 4 2007/3
2006 10 2006/10
Year_month
200902
200903
200704
200610
我认为你可以使用:
每列中的值是什么类型的?字符串,整数?同样,这是熊猫数据帧类型(文件名['month'])输出[3]:pandas.core.series.series类型(RepairTrain['year'])输出[3]:pandas.core.series.seriesnop。字符串pandas.Series不提供
zfill
。请参阅链接。我想这是可能的。哇,我发誓我只是失败了。你说得对!没问题。天气真好。很高兴能帮助你!祝你好运
def f(x, y)
return x*100+y
for i in range(0,filename.shape[0]):
filename['Year_month'][i] = f(filename['year'][i] ,filename['month'][i])
df['Year_month'] = df.Year.astype(str) + df.Month.astype(str).str.zfill(2)
print df
Year Month Year_month
0 2009 2 200902
1 2009 3 200903
2 2007 4 200704
3 2006 10 200610
df = df.read_clipboard()
Year Month Year_month
0 2009 2 2009/2
1 2009 3 2009/3
2 2007 4 2007/3
3 2006 10 2006/10
df['Year_month'] = df.apply(lambda row: str(row.Year)+str(row.Month).zfill(2), axis=1)
Year Month Year_month
0 2009 2 200902
1 2009 3 200903
2 2007 4 200704
3 2006 10 200610