pandas combine two columns with null values

2024/11/20 12:44:10

I have a df with two columns and I want to combine both columns ignoring the NaN values. The catch is that sometimes both columns have NaN values in which case I want the new column to also have NaN. Here's the example:

df = pd.DataFrame({'foodstuff':['apple-martini', 'apple-pie', None, None, None], 'type':[None, None, 'strawberry-tart', 'dessert', None]})df
Out[10]:
foodstuff   type
0   apple-martini   None
1   apple-pie   None
2   None    strawberry-tart
3   None    dessert
4   None    None

I tried to use fillna and solve this :

df['foodstuff'].fillna('') + df['type'].fillna('')

and I got :

0      apple-martini
1          apple-pie
2    strawberry-tart
3            dessert
4                   
dtype: object

The row 4 has become a blank value. What I want in this situation is a NaN value since both the combining columns are NaNs.

0      apple-martini
1          apple-pie
2    strawberry-tart
3            dessert
4            None       
dtype: object
Answer

Use fillna on one column with the fill values being the other column:

df['foodstuff'].fillna(df['type'])

The resulting output:

0      apple-martini
1          apple-pie
2    strawberry-tart
3            dessert
4               None
https://en.xdnf.cn/q/26324.html

Related Q&A

Python equivalent of zip for dictionaries

If I have these two lists:la = [1, 2, 3] lb = [4, 5, 6]I can iterate over them as follows:for i in range(min(len(la), len(lb))):print la[i], lb[i]Or more pythonicallyfor a, b in zip(la, lb):print a, bW…

Can I prevent fabric from prompting me for a sudo password?

I am using Fabric to run commands on a remote server. The user with which I connect on that server has some sudo privileges, and does not require a password to use these privileges. When SSHing into th…

Managing connection to redis from Python

Im using redis-py in my python application to store simple variables or lists of variables in a Redis database, so I thought it would be better to create a connection to the redis server every time I n…

Writing a Python extension in Go (Golang)

I currently use Cython to link C and Python, and get speedup in slow bits of python code. However, Id like to use goroutines to implement a really slow (and very parallelizable) bit of code, but it mus…

Non-global middleware in Django

In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I wan…

I want to replace single quotes with double quotes in a list

So I am making a program that takes a text file, breaks it into words, then writes the list to a new text file.The issue I am having is I need the strings in the list to be with double quotes not singl…

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?…

Pipfile.lock out of date

Im trying to deploy a large django project to heroku. I installed Heroku CLI, logged in, created an app and ran:git push heroku masterI have a Pipfile and requirements.txt already set up. I added a run…

Can a simple difference in Python3 variable names alter the way code runs? [duplicate]

This question already has answers here:Python attributeError on __del__(2 answers)Closed 9 years ago.This code...class Person:num_of_people = 0def __init__(self, name):self.name = namePerson.num_of_peo…

Easy way to check that a variable is defined in python? [duplicate]

This question already has answers here:How do I check if a variable exists?(15 answers)Closed 10 years ago.Is there any way to check if a variable (class member or standalone) with specified name is d…