Создать Series
, а затем преобразовать в DataFrame
:
QuandlEIASectorList = (pd.Series(QuandlEIASector)
.rename_axis('SectorDescription')
.reset_index(name='SectorCode'))
Аналогично:
QuandlEIASectorList = (pd.Series(QuandlEIASector, name='SectorCode')
.rename_axis('SectorDescription')
.reset_index())
Ваш код должен использоваться с конструктором DataFrame
:
QuandlEIASectorList = pd.DataFrame({'SectorDescription':list(QuandlEIASector.keys()),
'SectorCode': list(QuandlEIASector.values())})
Или:
QuandlEIASectorList = pd.DataFrame(list(QuandlEIASector.items()),
columns=['SectorDescription','SectorCode'])
Производительность для клавиш 10k:
QuandlEIASector = dict(zip([f'{x} data' for x in np.arange(10000)],
[f'{x} keys' for x in np.arange(10000)]))
In [73]: %%timeit
...: QuandlEIASectorList = pd.DataFrame()
...: QuandlEIASectorList['SectorDescription'] = QuandlEIASector.keys()
...: QuandlEIASectorList['SectorCode'] = QuandlEIASector.values()
...:
5.94 ms ± 52.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [74]: %%timeit
...: (pd.Series(QuandlEIASector)
...: .rename_axis('SectorDescription')
...: .reset_index(name='SectorCode'))
...:
5.37 ms ± 261 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [75]: %%timeit
...: (pd.Series(QuandlEIASector, name='SectorCode')
...: .rename_axis('SectorDescription')
...: .reset_index())
...:
5.34 ms ± 211 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [76]: %%timeit
...: pd.DataFrame({'SectorDescription':list(QuandlEIASector.keys()),
...: 'SectorCode': list(QuandlEIASector.values())})
...:
2.26 ms ± 20.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [77]: %%timeit
...: pd.DataFrame(list(QuandlEIASector.items()),
...: columns=['SectorDescription','SectorCode'])
...:
3.15 ms ± 38.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)