Python: Split pandas dataframe column containing a list and a value into two columns

pandas split column into multiple columns by value
pandas split column into multiple rows
pandas split dataframe by column name
pandas split column of lists into multiple rows
pandas split dataframe by column value
pandas dataframe split
pandas split list into columns
pandas split column by multiple delimiter

I have a Pandas data frame with a column that contains a list and a value: ([z, z, z, z, m, ., c, l, u, b, .], 0.0)

How do I split this column into two columns that I add to the data frame? The output I want: one column will contain the list, the other column will contain the value. For example:

[z, z, z, z, m, ., c, l, u, b, .] and 0.0

I have tried str.split(...,expand=True,) but the output is just a column of NaN. I can't use the comma delimiter and ], both produce one column of NaN rather than a column of lists and a column of values.

Here's 4 rows of the column of my Pandas data frame that I'm trying to manipulate.

X['set']
1                  ([z, z, z, z, m, ., c, l, u, b, .], 0.0)
2                  ([z, z, z, z, g, ., c, l, u, b, .], 0.0)
3              ([z, z, z, z, cy, s, ., l, o, a, n, .], 0.0)
4                        ([z, z, z, x, c, ., u, s, .], 0.0)

I was able to figure it out based on deduction using the answers of other users.

pd.DataFrame(X['set'].tolist(), index=df.index)

Related post: how to split column of tuples in pandas dataframe?

How to Split a Column into Two Columns in Pandas?, How do I split a column into multiple columns in pandas? If you wanted to split a column of delimited strings rather than lists, you could similarly do: df["teams"].str.split('<delim>', expand=True) already returns a DataFrame, so it would probably be simpler to just rename the columns.

can you try making the delimiter ],?

How to split a pandas DataFrame into multiple DataFrames by , How do you split data in one column into multiple columns in Python? Python | Pandas Split strings into two List/Columns using str.split() Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string.

You just need a bit of string gymnastics:

def separate(x):
    closing_bracket_index = x.index(']')
    list_vals = x[:closing_bracket_index+1]
    val = x[closing_bracket_index+3:]

    return pd.Series([list_vals, val], index=['list', 'val'])

X['set'].apply(separate)

How to split a data frame?, How do you split a Dataframe in Python based on column values? We can use Pandas’ str.split function to split the column of interest. Here we want to split the column “Name” and we can select the column using chain operation and split the column with expand=True option. str.split () with expand=True option results in a data frame and without that we will get Pandas Series object as output. 1

Hope this works

import numpy as np
import pandas as pd

a = (['g','f'],0.0)
b = (['d','e'],0.1)
df = pd.DataFrame({'col':[a,b]})
df

Out[1]: 
             col
0  ([g, f], 0.0)
1  ([d, e], 0.1)

def split_val(col):
    list_val = col[0]
    value    = col[1]
    return pd.Series([list_val, value], index=['list', 'val'])


df[['list_val','value']] = df['col'].apply(split_val) 
df

Out[2]: 
             col list_val  value
0  [[g, f], 0.0]   [g, f]    0.0
1  [[d, e], 0.1]   [d, e]    0.1

Split a text column into two columns in Pandas DataFrame , It contains well written, well thought and well explained computer science and Let's see how to split a text column into two columns in Pandas DataFrame. Use underscore as delimiter to split the column into two columns. into a single column in Pandas · Python | Pandas Split strings into two List/Columns using str.​split  # step 1: convert the `Pollutants` column to Pandas dataframe series df_pol_ps = data_df['Pollutants'].apply(pd.Series) df_pol_ps: a b c 0 46 3 12 1 36 5 8 2 NaN 2 7 3 NaN NaN 11 4 82 NaN 15 # step 2: concat columns `a, b, c` and drop/remove the `Pollutants` df_final = pd.concat([df, df_pol_ps], axis = 1).drop('Pollutants', axis = 1) df_final

Python: Split pandas dataframe column containing a list and a value , I was able to figure it out based on deduction using the answers of other users. pd.DataFrame(X['set'].tolist(), index=df.index). Related post: how  In case someone else wants to split a single column (deliminated by a value) into multiple columns - try this: series.str.split(',', expand=True) This answered the question I came here looking for. Credit to EdChum's code that includes adding the split columns back to the dataframe. pd.concat([df[[0]], df[1].str.split(', ', expand=True)], axis=1)

How to split a pandas DataFrame column in Python, Let us first create a simple Pandas data frame using Pandas' DataFrame function. 1. 2. 3. 4. 5. 6. 7. Essentially, we would like to select rows based on one value or multiple values present in a column. Here are SIX examples of using Pandas dataframe to filter rows or select rows based values of a column(s). Let us first load gapminder data as a dataframe into pandas.

pandas.Series.str.split, How to split a pandas DataFrame column in Python. Splitting a pandas.​DataFrame column will convert one column into two columns. pandas.Series to a list. You then decided to capture that data in Python using pandas DataFrame. At a certain point, you realize that you’d like to convert that pandas DataFrame into a list. To accomplish this goal, you may use the following Python code, which will allow you to convert the DataFrame into a list, where:

Comments
  • what is your expected\ output ?
  • Doesn't work. ValueError: tuple.index(x): x not in tuple. My Series is not a list of list_vals and values. It's a tuple.
  • This doesn't work. Your df has square brackets for each row whereas mine has parens, so your example uses a list whereas mine is a tuple.
  • @user2205916 this works as well, but your code is cleaner :)