Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Performance-warning when operating on dataframe

This code results in a performance warning, but i have a hard time optimizing it.

for i in range(len(data['Vektoren'][0])):
    tmp_lst = []
    for v in data['Vektoren']:
        tmp_lst.append(v[i])
    data[i] = tmp_lst

DataFrame is highly fragmented. This is usually the result of calling frame.insert many times, which has poor performance. Consider joining all columns at once usi
ng pd.concat(axis=1) instead. To get a de-fragmented frame, use newframe = frame.copy()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You seem to want to convert your Series of lists/arrays into several columns.

Rather use:

data = data.join(pd.DataFrame(data['Vektoren'].tolist(), index=data.index))

Or:

data = pd.concat([data, pd.DataFrame(data['Vektoren'].tolist(), index=data.index)],
                 axis=1)

Example output:

       Vektoren    0    1    2    3
0  [1, 2, 3, 4]  1.0  2.0  3.0  4.0
1        [5, 6]  5.0  6.0  NaN  NaN
2            []  NaN  NaN  NaN  NaN

Used input:

data = pd.DataFrame({'Vektoren': [[1,2,3,4],[5,6],[]]})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading