Split lists within dataframe column into multiple columns [duplicate]

2024/9/24 8:26:55

I have a Pandas DataFrame column with multiple lists within a list. Something like this:

dfcol1
0    [[1,2], [2,3]]
1    [[a,b], [4,5], [x,y]] 
2    [[6,7]]

I want to split the list over multiple columns so the output should be something like:

    col1    col2     col3
0   [1,2]   [2,3]   
1   [a,b]   [4,5]    [x,y]
2   [6,7]

Please help me with this. Thanks in advance

Answer

You can use pd.Series.apply:

df = pd.DataFrame({'col1': [[[1, 2], [2, 3]],[['a', 'b'], [4, 5], ['x', 'y']],[[6, 7]]]})res = df['col1'].apply(pd.Series)print(res)0       1       2
0  [1, 2]  [2, 3]     NaN
1  [a, b]  [4, 5]  [x, y]
2  [6, 7]     NaN     NaN
https://en.xdnf.cn/q/71726.html

Related Q&A

Drawing with turtle(python) using PyCharm

Im running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesnt seem to work import turtlewn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) a…

How to adapt my current splash screen to allow other pieces of my code to run in the background?

Currently I have a splash screen in place. However, it does not work as a real splash screen - as it halts the execution of the rest of the code (instead of allowing them to run in the background).This…

Reversed array slice including the first element [duplicate]

This question already has answers here:Python reverse-stride slicing(8 answers)Closed 5 years ago.Lets say I have:>>> a = [1, 2, 3, 4]And I want to get a reversed slice. Lets say I want the 1s…

Problem using py2app with the lxml package

I am trying to use py2app to generate a standalone application from some Python scripts. The Python uses the lxml package, and Ive found that I have to specify this explicitly in the setup.py file that…

How to run TensorFlow on AMD/ATI GPU?

After reading this tutorial https://www.tensorflow.org/guide/using_gpu I checked GPU session on this simple code import numpy as np import matplotlib.pyplot as plt import tensorflow as tfa = tf.constan…

Pure virtual function call

Im using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this:class Base {virtual int getPosition() = 0; };boost::pytho…

Expected String or Unicode when reading JSON with Pandas

I try to read an Openstreetmaps API output JSON string, which is valid.I am using following code:import pandas as pd import requests# Links unten minLat = 50.9549 minLon = 13.55232# Rechts oben maxLat …

How to convert string labels to one-hot vectors in TensorFlow?

Im new to TensorFlow and would like to read a comma separated values (csv) file, containing 2 columns, column 1 the index, and column 2 a label string. I have the following code which reads lines in th…

Pandas dataframe boolean mask on multiple columns

I have a dataframe (df) containing several columns with an actual measure and corresponding number of columns (A,B,...) with an uncertainty (dA, dB, ...) for each of these columns:A B dA dB …

Which of these scripting languages is more appropriate for pen-testing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…