2

I have a pandas dataframe df, which has only one column col. I want to loop values of col, and add columns to fill values by using the values of the first column col. For example, the first row is a list, which has 3 elements ['text1','text2','text3']. I want to add 3 columns, and fill values using 'text1','text2' and 'text3'.

import pandas as pd

df=pd.DataFrame({'col':[['text1','text2','text3'],['mext1','mext2'],['cext1']]})
df

    col
0   [text1, text2, text3]
1   [mext1, mext2]
2   [cext1]

I want like this:

    col                     col_1     col_2     col_3
0   [text1, text2, text3]   text1     text2     text3
1   [mext1, mext2]          mext1     mext2     Nan
2   [cext1]                 cext1     Nan       Nan    

Your help will be appreciated.

2 Answers 2

3

Another solution with DataFrame constructor, where need rename columns and add_prefix:

print (pd.DataFrame(df.col.values.tolist(), index=df.col)
         .rename(columns = lambda x: x+1)
         .add_prefix('col_')
         .reset_index())

                     col  col_1  col_2  col_3
0  [text1, text2, text3]  text1  text2  text3
1         [mext1, mext2]  mext1  mext2   None
2                [cext1]  cext1   None   None

Solution where find max length of list in column col by str.len:

cols = df.col.str.len().max() + 1
print (cols)
4
print (pd.DataFrame(df.col.values.tolist(), index=df.col,columns = np.arange(1, cols))
         .add_prefix('col_')
         .reset_index())
                     col  col_1  col_2  col_3
0  [text1, text2, text3]  text1  text2  text3
1         [mext1, mext2]  mext1  mext2   None
2                [cext1]  cext1   None   None
Sign up to request clarification or add additional context in comments.

Comments

3

You could construct a new dataframe by converting the values present in the single column to it's list representation form. The elements of the list would now become separate column entities in itself.

These could then be concatenated with the original DF columnwise (axis=1).

df_expand = pd.DataFrame(df['col'].tolist(), df.index)
df_expand.columns = df_expand.columns + 1
pd.concat([df['col'], df_expand.add_prefix('col_')], axis=1)

enter image description here

To get None to be represented as NaN, you could add .replace({None:np.NaN}) at the end of the last syntax.

1 Comment

Does not work for df = pd.DataFrame({'col':[['text1','text2','text3'],['mext1','mext2'],['cext1'],['cext2']]}). Problem: np.arange(1, df.shape[0] + 1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.