Улучшение производительности нескольких вызовов s.str.splitPython

Программы на Python
Anonymous
Улучшение производительности нескольких вызовов s.str.split

Сообщение Anonymous »

DataFrame имеет следующие столбцы

Код: Выделить всё

NCT Number                            object
Study Title                           object
Study URL                             object
Acronym                               object
Study Status                          object
Brief Summary                         object
Study Results                         object
Conditions                            object
Interventions                         object
Primary Outcome Measures              object
Secondary Outcome Measures            object
Other Outcome Measures                object
Sponsor                               object
Collaborators                         object
Sex                                   object
Age                                   object
Phases                                object
Enrollment                           float64
Funder Type                           object
Study Type                            object
Study Design                          object
Other IDs                             object
Start Date                    datetime64[ns]
Primary Completion Date       datetime64[ns]
Completion Date               datetime64[ns]
First Posted                  datetime64[ns]
Results First Posted          datetime64[ns]
Last Update Posted            datetime64[ns]
Locations                             object
Study Documents                       object
dtype: object
Столбец вмешательства.head(10):

Код: Выделить всё

0             DEVICE: Kinesio(R)Tape for edema control
1                                       DRUG: Ramipril
2             DRUG: Dexamethasone intravitreal implant
3         DRUG: Nivolumab 480mg and surgical resection
4      OTHER: group 40|OTHER: group 50|OTHER: group 60
5                                          OTHER: Diet
6             DRUG: Triamcinolone|DRUG: Turmeric paste
7    DIAGNOSTIC_TEST: DNA methylation|DIAGNOSTIC_TE...
8                             DRUG: CS0159 (Linafexor)
9    BIOLOGICAL: Short Ragweed Pollen Allergenic Ex...
Name: Interventions, dtype: object
  • Разделить | (но не расширять на новые столбцы, не каждый элемент имеет одинаковое разделение)
  • Разбить на новые строки
  • Разбить по строкам сначала:
  • Развернуть на 2 столбца
Я написал

Код: Выделить всё

interventions.str.split('|',expand=False).explode().str.split(': ', expand=True, n=1)
но мне любопытно, есть ли более эффективный подход. Вывод cProfile в одну строку:

Код: Выделить всё

 6314077 function calls (6314057 primitive calls) in 9.940 seconds

Ordered by: call count

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1820581/1820567    0.562    0.000    0.562    0.000 {built-in method builtins.len}
910260    0.549    0.000    0.781    0.000 accessor.py:348()
910259    0.496    0.000    0.496    0.000 accessor.py:339(cons_row)
859697    2.039    0.000    3.210    0.000 object_array.py:354()
859697    1.115    0.000    1.115    0.000 {method 'split' of 're.Pattern' objects}
457375    0.390    0.000    0.390    0.000 {method 'split' of 'str' objects}
457372    0.514    0.000    0.934    0.000 object_array.py:358()
14021    0.013    0.000    0.013    0.000 threading.py:1196(ident)
8012    0.005    0.000    0.005    0.000 {method 'keys' of 'dict' objects}
4182/4181    0.002    0.000    0.002    0.000 {built-in method builtins.isinstance}
4007    0.003    0.000    0.003    0.000 {method 'values' of 'dict' objects}
4006    0.052    0.000    0.097    0.000 ipkernel.py:775(_clean_thread_parent_frames)
2003    0.021    0.000    0.026    0.000 threading.py:1533(enumerate)
2003    0.002    0.000    0.002    0.000 {method '__exit__' of '_thread.RLock' objects}
45    0.000    0.000    0.000    0.000 {built-in method builtins.getattr}
18    0.000    0.000    0.000    0.000 typing.py:2173(cast)
17    0.000    0.000    0.000    0.000 generic.py:42(_instancecheck)
17    0.000    0.000    0.000    0.000 generic.py:37(_check)
16    0.000    0.000    0.000    0.000 {built-in method builtins.hasattr}
11    0.000    0.000    0.000    0.000 {built-in method builtins.issubclass}

На данный момент каждая строка должна иметь одно вмешательство.
Поскольку каждое вмешательство похоже на dict, возможно ли это преобразовать каждое вмешательство в dict и расширить его до новых столбцов? Очевидно, пытаюсь выполнить это векторно.

Подробнее здесь: https://stackoverflow.com/questions/790 ... plit-calls

Вернуться в «Python»