merge two dataframe columns into 1 in pandas

2024/10/4 17:30:25

I have 2 columns in my data frame and I need to merge it into 1 single column

Index  A             Index   B
0      A             0       NAN
1      NAN           1       D
2      B             2       NAN
3      NAN           3       E
4      C             4       NAN

there will always be one 1 value across the Columns, i want my result to look like

0    A
1    B
2    C
3    D
4    E
Answer

Option 1

df.stack().dropna().reset_index(drop=True)0    A
1    D
2    B
3    E
4    C
dtype: object

Option 2 If Missing values are always alternating

df.A.combine_first(df.B)Index
0    A
1    D
2    B
3    E
4    C
Name: A, dtype: object

Option 3 What you asked for

df.A.append(df.B).dropna().reset_index(drop=True)0    A
1    B
2    C
3    D
4    E
dtype: object

Option 4 Similar to 3 but over arbitrary number of columns

pd.concat([i for _, i in df.iteritems()]).dropna().reset_index(drop=True)0    A
1    B
2    C
3    D
4    E
dtype: object
https://en.xdnf.cn/q/70587.html

Related Q&A

Upsample and Interpolate a NumPy Array

I have an array, something like:array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an…

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…

numpy.savetxt tuple index out of range?

Im trying to write a few lines into a text file, and heres the code I used:import numpy as np# Generate some test data data = np.arange(0.0,1000.0,50.0)with file(test.txt, w) as outfile: outfile.w…

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…

Django - Stream request from external site as received

How can Django be used to fetch data from an external API, triggered by a user request, and stream it directly back in the request cycle without (or with progressive/minimal) memory usage?BackgroundAs…

django rest framework - always INSERTs, never UPDATES

I want to be able to UPDATE a user record by POST. However, the id is always NULL. Even if I pass the id it seems to be ignoredView Code:JSON POSTED:{"id": 1, "name": "Craig Ch…

Copy fields from one instance to another in Django

I have the following code which takes an existing instance and copies, or archives it, in another model and then deletes it replacing it with the draft copy. Current Codedef archive_calc(self, rev_num,…

Python selenium get Developer Tools →Network→Media logs

I am trying to programmatically do something that necessarily involves getting the "developer tools"→network→media logs. I will spare you the details, long story short, I need to visit thou…