Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 迭代3个数据帧列的最短方法_Python_Pandas_Dataframe - Fatal编程技术网

Python 迭代3个数据帧列的最短方法

Python 迭代3个数据帧列的最短方法,python,pandas,dataframe,Python,Pandas,Dataframe,执行以下操作的最短方法是什么 i = 0 for Year, Month, Day in zip(test_data['Year'], test_data['Month'], test_data['Day']): ans = dt.date(Year, Month, Day) test_data.loc[i,'Day1'] = ans.strftime("%A") i += 1 您可以在行上应用,,这样可以避免压缩,并跟踪您所处的行: df = pd.DataFrame

执行以下操作的最短方法是什么

i = 0
for Year, Month, Day in zip(test_data['Year'], test_data['Month'], test_data['Day']):
    ans = dt.date(Year, Month, Day)
    test_data.loc[i,'Day1'] = ans.strftime("%A")
    i += 1

您可以在行上应用
,这样可以避免
压缩
,并跟踪您所处的行:

df = pd.DataFrame({'Year': [2015, 2016], 'Month': [12, 1], 'Day': [28, 3]})

df
Out[3]: 
   Day  Month  Year
0   28     12  2015
1    3      1  2016

df['Day1'] = df.apply(
    lambda row: dt.date(row['Year'], row['Month'], row['Day']).strftime('%A'),
    axis='columns')
输出:

df
Out[11]: 
   Day  Month  Year    Day1
0   28     12  2015  Monday
1    3      1  2016  Sunday

你如何定义最短的?执行时间方面?嗨,大卫,是的,执行时间方面。对不起,问题中没有具体说明。马吕斯的评论正好回答了这个问题。谢谢马吕斯。