To get another order of increasing the speed of Wen Answer , we can use our own iterators, for example:
The code:
index_as_dict = {}
for k, v in index.ravel():
index_as_dict.setdefault(k, []).append(v)
pd.Series(index_as_dict)
Security Code:
import pandas as pd
df = pd.read_fwf(StringIO(u"""
level_0 level_1 val
a dog 1
a cat 2
b fox 3
b rat 4"""), header=1).set_index(['level_0', 'level_1'])
print(df)
def method1():
return df.reset_index(level=1).groupby(level=0)['level_1'].apply(list)
def method2():
index_as_dict = {}
for k, v in df.index.ravel():
index_as_dict.setdefault(k, []).append(v)
return pd.Series(index_as_dict)
print(method1())
print(method2())
from timeit import timeit
print(timeit(method1, number=50))
print(timeit(method2, number=50))
Results:
val
level_0 level_1
a dog 1
cat 2
b fox 3
rat 4
level_0
a [dog, cat]
b [fox, rat]
Name: level_1, dtype: object
a [dog, cat]
b [fox, rat]
dtype: object
0.0760027870983045
0.006749932432252637
source
share