2

Suppose I have a variable value:

value = [array([   3,    0,     1, -9999]),
         array([   1,    8,     5, -9999])]

How to write these values as separate columns D, E, F and Gin pandas existing dataframe data. where data is

   A   B   C
0  2   6   1 
1  9   2   7

Expected Output:

    A   B  C  D  E  F  G
0   2   6  1  3  0  1  -9999
1   9   2  7  1  8  5  -9999

1 Answer 1

2

Apparently this works, who knew it was that simple!

df[['D', 'E', 'F']] = pd.DataFrame(value, index=df.index)
df        

   A  B  C  D  E  F
0  2  6  1  3  0  1
1  9  2  7  1  8  5
Sign up to request clarification or add additional context in comments.

5 Comments

Is there any fastest alternative? which avoids creation of an extra dataframedf2.
@Ganesh Okay, take a look.
I am getting this error: KeyError: "None of [Index(['D', 'E', 'F'], dtype='object')] are in the [columns]"
@Ganesh I should also mention this worked for me in pandas 1.0, so it's possible version incompatibility is also an issue.
May be. However, it worked when I added df[['D', 'E', 'F']] = pd.DataFrame(value). Thanks for the help!

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.