Dropping cell if it is NaN in a Dataframe in python

2024/10/6 9:22:57

I have a dataframe like this.

   Project 4    Project1    Project2    Project3
0   NaN         laptio          AB      NaN
1   NaN         windows         ten     NaN
0   one         NaN             NaN
1   two         NaN             NaN

I want to delete NaN values from Project 4 column

My desired output should be,

df,

   Project 4    Project1    Project2    Project3
0   one         laptio          AB      NaN
1   two         windows         ten     NaN
0                   NaN        NaN       NaN
1                NaN            NaN
Answer

If your data frame's index is just standard 0 to n ordered integers, you can pop the Project4 column to a series, drop the NaN values, reset the index, and then merge it back with the data frame.

import pandas a pddf = pd.DataFrame([[pd.np.nan, 1,2,3],[pd.np.nan, 4,5,6],['one',7,8,9],['two',10,11,12]], columns=['p4','p1','p2','p3'])s = df.pop('p4')
pd.concat([df, ps.dropna().reset_index(drop=True)], axis=1)# returns:p1  p2  p3   p4
0   1   2   3  one
1   4   5   6  two
2   7   8   9  NaN
3  10  11  12  NaN
https://en.xdnf.cn/q/118969.html

Related Q&A

How can I iterate through excel files sheets and insert formula in Python?

I get this error TypeError: Workbook object is not subscriptablewhen i run this code import xlsxwriter from openpyxl import load_workbookin_folder = rxxx #Input folder out_folder = rxxx #Output folde…

How to bind all frame widgets to Enter event

I the following code I want to bind all frame1 items to <Enter> Event, but it does not work. I mean canvas.focus_set() does not take effect. How can I solve my problem?for w in frame1.winfo_chil…

Typeerror takes no arguments [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Unable to access element within page

Form Screenshot HTML Inspect Code screenshotIm trying to access an element within a page. Cannot give out the exact page link owing to security concerns. Im writing a python program that uses selenium …

How to wtite Erlang B and Erlang C formulas in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pip is not recognizing the install command (Windows 7, Python 3.3) [duplicate]

This question already has answers here:python3 --version shows "NameError: name python3 is not defined" [duplicate](2 answers)Closed 6 years ago.I am trying to install Python programs using P…

How do I check if 1 is always followed by a 0

In Python, I cannot find a solution as to how to determine whether there is always a 0 following a 1 somewhere in a list of numbers, to form a pair 10. It doesnt have to be direct follower.For clarity,…

Visual Studio Code debugs even when i run without debugging

i just installed VSCode and I used to work on it but now when I try to run without Debugging with Ctrl + F5. it seems to opens up python debug console and debug like below image enter image description…

Mock global function call while importing

Suppose I have a file called a.py with code likeimport mod1 mod1.a()def b():print("hi")Now if I want to mock fun b() then unittest.py while have import statement at top likefrom a import bat …

can the order of code make this program faster?

Hi this is my first post, I am learning how to write code so technically I am a newbie.I am learning python I am still at the very basics, I was getting to Know the if statement and I tried to mix it …