Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 转换大型数据帧-花费的时间太长_Python_Pandas_Dataframe - Fatal编程技术网

Python 转换大型数据帧-花费的时间太长

Python 转换大型数据帧-花费的时间太长,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个从CSV加载的数据帧,格式如下: stock_code price 20180827 001 10 20180827 002 11 20180827 003 12 20180827 004 13 20180826 001 14 20180826 002 15 20180826 003

我有一个从CSV加载的数据帧,格式如下:

           stock_code    price 
20180827     001          10
20180827     002          11
20180827     003          12
20180827     004          13
20180826     001          14
20180826     002          15
20180826     003          11
20180826     004          10
20180826     005          19
            001     002     003     004     005
20180827    10      11      12      13      nan
20180826    14      15      11      10      19
我想将其转换为以下格式:

           stock_code    price 
20180827     001          10
20180827     002          11
20180827     003          12
20180827     004          13
20180826     001          14
20180826     002          15
20180826     003          11
20180826     004          10
20180826     005          19
            001     002     003     004     005
20180827    10      11      12      13      nan
20180826    14      15      11      10      19
这是我的函数(
oracle_data
是原始数据帧),用于进行转换,但547500行数据帧需要7分钟。有没有办法加快速度

def transform_data(oracle_data):
    data_code = oracle_data[0]  
    data_date = oracle_data[1] 
    factor_date = sorted(data_date.unique()) 
    stock_list =  sorted(data_code.unique())     
    factor_data = pd.DataFrame(index = factor_date, columns = stock_list)
    sort = oracle_data.sort_index()
    for n in oracle_data.index:
        factor_data.at[oracle_data.at[n,1],oracle_data.at[n,0]]=oracle_data.at[n,2]
    return factor_data

我相信以下是可能的用途:


你的职能是什么?你能分享吗?@Amoxz给你-我想pd.melt可以帮你。嗨,请通过链接。似乎是一个重复的问题。你可以为你的问题找到多个答案。非常好用,谢谢much@Amoxz-不客气!如果我的答案有帮助,别忘了——点击答案旁边的复选标记,将其从灰色变为填充。谢谢