In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes the relevant knowledge of what is the method for pandas to return the missing value position, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "what is the way for pandas to return missing values?"
Pandas returns the position of missing values
Sometimes we may need to get some information about missing values, so we need to get the location of these missing values in the DataFrame.
If the index of our DataFrame is a numeric sequential index, we want to return the position of the missing value
Import numpy as npimport pandas as pd
Let's first build a DataFrame with missing values
Df = pd.DataFrame ({'Aids: [1, 2, 3, np.nan],' np.nan: [2, np.nan, 4, 6], 'np.nan: [3, 2, np.nan, 3],' dating: [np.nan, 1, np.nan, 2]}) display (df)
First we can see if there are any missing values in each column
Df.isnull () .any ()
We find that all four columns have missing values. For example, we want to know the exact location of the missing values in each column. Take An as an example.
Df.isnull () .any ()
You can see the index that returns the location of the missing value of A, so that when locating, you can use the loc function to locate the missing value directly for easy viewing.
If we want to generate a dictionary to store all the missing value location information, we can use:
A = df.isnull () .any () a = a.loco [a = = True] columns = a.index.tolist () mydict = {} # create a dictionary to store all locations temp = [] for column in columns: temp = df.loc.df [column] .isnull () .index.ist () mydict [column] = tempmydict
Attached: Pandas looks up the location of the missing value And return the missing value line number and column number #! / usr/bin/evn python#-*-coding:utf8-*-import pandas as pddata = [[None, None, 90, 80], [57, 43, 89, 65], [78, 50, 67, 78], [67, 45, 78, 76], [77, 88, None, 45], [52, 110, 120, 99], [131,13, 32, 12]] index = ['language', 'English'. Mathematics, Politics, Physics, Chemistry, Biology, Geography] column = ['Zhang San','Li Si', 'Wang Wu', 'Saturday'] data = pd.DataFrame (data,index=index Columns=column) print (data) print ("=\ n") for columname in data.columns: if data [columname] .count ()! = len (data): loc = data [columname] [data [columnname] .isnull () .values = = True] .index.tolist () print ('column name: "{}", line {} has missing value' .format (columname,loc))
Execution result
F:\ Opensources\ python\ python.exe D:/pythonStudy/EXCELDB/LagelangriCZ_test.py
Zhang San Li Si Wang Wu Saturday
Chinese NaN NaN 90.080
English 57.0 43.0 89.0 65
Mathematics 78.0 50.0 67.0 78
Political NaN 78.090.073
Physics 67.0 45.0 78.0 76
Chemistry 77.088.0 NaN 45
Biology 52.0 110.0 120.0 99
Geography 131.0 13.0 32.0 12
=
List: "Zhang San", there is a missing value in line ['Chinese', 'politics'].
List: "Li Si", there is a missing value in line ['Chinese'].
List: "Wang Wu", there is a missing value in line ['chemistry'].
Process finished with exit code 0
This article mainly analyzes the relevant knowledge of what is the method for pandas to return the missing value position, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "what is the way for pandas to return missing values?"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.