python-3.x - 数据分析 - 如何计算空值、NaN 和空字符串值?
问题描述
我是 pyspark 的新手,我有这个示例数据集:
Ticker_Modelo Ticker Type Period Product Geography Source Unit Test
0 Model1_Index Model1 Index NWE Forties Hydrocraking Daily Refinery Margins NWE Bloomberg None 3
1 Model2_Index Model2 Index NWE Bonny Light Hydrocraking Daily Refinery Margins NWE Bloomberg None 5
2 Model3_Index Model3 Index USGC LLS FCC Daily Refinery Margins USGC Bloomberg None 12
3 Model4_Index Model4 Index USGC Maya Coking Daily Refinery Margins USGC Bloomberg None 67
4 Model6_Index Model6 Index USMC WTI FCC Daily Refinery Margins USMC Bloomberg None 45
5 Model5_Index Model5 Index USMC WCSS Coking Daily Refinery Margins USMC Bloomberg None 22
6 Model7_Index Model7 Index USEC Hibernia FCC Daily Refinery Margins USEC Bloomberg None
7 Model8_Index Model8 Index Singapore Dubai Hydrocracking Daily Refinery Margins Singapore Bloomberg None Null
我需要进行数据分析并将其存储在数据库中。
我尝试过使用 Optimus ( https://github.com/ironmussa/Optimus/ ) 和 panda_profiler ( https://pandas-profiling.github.io/pandas-profiling/docs/ ) 但他们会进行分析并为您提供一个 HTML,我需要一些它不计算的值。
我需要计算每列中有多少 null/NaN/空字符串,并用它创建一个新表。
我正在使用熊猫和 pyspark。
我找到了一个我认为可以提供帮助的答案Python / Pyspark - Count NULL, empty and NaN,但是当我尝试将它应用到一列尝试时
data_df.filter((data_df["Ticker_Modelo"] == "") | data_df["Ticker_Modelo"].isNull() | isnan(data_df["Ticker_Modelo"])).count()
它给了我一个错误:AttributeError: 'Series' object has no attribute 'isNull'
然后我不知道如何将它应用到所有列并转置它以获得如下内容:
Count_nulls
Ticker_Modelo 0
Ticker 0
Type 0
Period 0
Product 0
Geography 0
Source 0
Unit 0
Test 2
解决方案
您可以执行以下操作:
首先将所有 Null/None 值更改为 Panda NaN
df.replace(['None','Null'],np.nan)
df.isnull().sum(axis=0).to_frame().rename(columns={0 : 'Count_Nulls'})