简体   繁体   English

如何从传递到 python 中的 dataframe 的列表中删除空白和 null 值

[英]How to remove blank and null values from a list that passed into dataframe in python

I have dataframe column like below.我有 dataframe 列,如下所示。

df['lane']
AZ
NL

NaN
BL
AZ

My code我的代码

unique_lane = df['lane'].unique()
unique_lane = pd.DataFrame( list(zip(unique_lane)), columns =['unique_lane'])
t = ', '.join(unique_lane['unique_lane'].astype(str))

While I am passing unique list values blank('') or Null values should be removed from the list.当我传递唯一列表值时,应从列表中删除空白('')或 Null 值。 The list t created should contain not blank or not Null values.创建的列表 t 不应包含空白或 Null 值。

bigdata_null_zones = bigdata_null_zones[~bigdata_null_zones["lane"].isin([t])]

How can this be done in python?如何在 python 中做到这一点?

Sample data for test DataFrame from question:来自问题的测试 DataFrame 的样本数据:

df = pd.DataFrame({'lane':['AZ','NL','', np.nan, 'BL','AZ']})

Test for pass only misisng values or empty strings:测试是否仅通过错误值或空字符串:

df = pd.DataFrame({'lane':['', np.nan]})
print (df)
  lane
0     
1  NaN

bigdata_null_zones = pd.DataFrame({'lane':['AZ','NL','AB', 'BL','AZ']})
print (bigdata_null_zones)
  lane
0   AZ
1   NL
2   AB
3   BL
4   AZ

After remove it get empty Series :删除后得到空Series

t = df['lane'].replace('',np.nan).dropna()
print (t)
Series([], Name: lane, dtype: float64)

So if pass get same values, because nothing filtered:所以如果通过得到相同的值,因为没有过滤:

bigdata_null_zones[bigdata_null_zones["lane"].isin(t)]
print (bigdata_null_zones)
  lane
0   AZ
1   NL
2   AB
3   BL
4   AZ

If same DataFrame:如果相同的 DataFrame:

df = pd.DataFrame({'lane':['AZ','NL','', np.nan, 'BL','AZ'],
                   'col':range(6)})

print (df)
  lane  col
0   AZ    0
1   NL    1
2         2
3  NaN    3
4   BL    4
5   AZ    5

df1 = df.assign(lane= df['lane'].replace('',np.nan)).dropna(subset=['lane'])
print (df1)
  lane  col
0   AZ    0
1   NL    1
4   BL    4
5   AZ    5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:在将 python 列表转换为 dataframe 时,传递了 4 列,传递的数据有 3 列。 如果 3 通过,如何添加空白值? - ValueError: 4 columns passed, passed data had 3 columns when converting python list to dataframe. How to add blank values if 3 passed? 使用 python 从嵌套列表中删除空白值 - Remove blank values from nested list using python 如何在 Python 中打印时从 Dataframe 的索引中删除空白行 - How to remove blank line from index of a Dataframe while printing in Python 如何从熊猫数据框中的csv中删除'\\ N'空值 - How to remove '\N' null values from csv from a pandas dataframe 如何从Python中的列表列表中删除值 - How to remove values from a list of list in Python 如何从我的列表和 python 中的元组中删除空白的引号? - How to remove blank inverted comma from my list and tuple in python? pandas从数据框中删除每个字段具有非空白值的行(Python 3.4 / IPython) - pandas Remove rows with non-blank values per a field from a dataframe (Python 3.4/IPython) 如何设置从 python 列表传递到 html 进度条的值 - How to set values passed from python list to a html progress bar 从列表python中删除所有空白行 - Remove all blank lines from list python 如何从 pandas dataframe 中删除不在列表中的某些值? - How to remove certain values from a pandas dataframe, which are not in a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM