简体   繁体   English

我应该如何从 python 中的 dataframe 中删除 nan 值?

[英]How should I remove nan values from a dataframe in python?

I've got an excel file and I created lists from its columns.我有一个 excel 文件,我从它的列中创建了列表。 The problem is the rows of the columns is not equal.问题是列的行不相等。 Therefore, I have multiple 'nan' values at ends of the lists.因此,我在列表末尾有多个“nan”值。 I tried to delete them with dropna() method but there are still the 'nan' values.我尝试使用 dropna() 方法删除它们,但仍然存在“nan”值。 Here is my code:这是我的代码:

import pandas as pd

excel_name = r'file_name.xlsx'
df = pd.read_excel(excel_name, engine='openpyxl')
df.dropna()

clomun_1 = list(df['clomun1'])
clomun_2 = list(df['clomun2'])
clomun_3 = list(df['clomun3'])
print(clomun_1)
print(clomun_2)
print(clomun_3)

output: output:

clomun_1 = ['value1', 'value2', 'value3', 'value4', 'nan', 'nan', 'nan', 'nan']
clomun_2 = ['value1', 'value2', 'value3', 'value4', 'value5', 'value6', 'nan', 'nan']
clomun_3 = ['value1', 'value2', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan']

I want to keep only values.我只想保留价值观。 I must delete "nan" elements.我必须删除“nan”元素。

Try this:尝试这个:

df = pd.read_excel(excel_name, engine='openpyxl', na_values=['nan']) #add na_values

clomun_1 = df['clomun1'].dropna().tolist()

print(clomun_1)

['value1', 'value2', 'value3', 'value4']

You can use a lambda function to achieve this.您可以使用 lambda function 来实现此目的。

clomun_1_new= [x for x in clomun_1 if x!='nan']

repeat the same for other two lists.对其他两个列表重复相同的操作。

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

相关问题 python-我如何从 pandas dataframe 中删除 2 列值的行(这些值应该是 2 个字符串的组合)? - python- How do i remove a rows from pandas dataframe by 2 columns value (The values should be a combination of 2 strings )? 使用 Python,如何在保留/忽略所有“nan”值的同时删除 PANDAS dataframe 列中的重复项? - Using Python, how do I remove duplicates in a PANDAS dataframe column while keeping/ignoring all 'nan' values? 从 pandas 数据框中删除特定的 nan 值 - remove specific nan values from pandas dataframe 从 Pandas 中删除 NaN 值 DataFrame - Remove NaN values from Pandas DataFrame 如何从python 3中的值的字典列表中删除nan值? - How to remove nan values from a dictionaries list of values in python 3? 当 notnull、dropna 和?= 'nan' 不起作用时,如何从 dataframe 列中删除 nan 值? - How to remove nan values from a dataframe column when notnull, dropna and != 'nan' don't work? 如何从 NumPy 数组中删除 NaN 值? - How do I remove NaN values from a NumPy array? 从 python 中的字典中删除 nan 值 - Remove nan values from a dict in python 我应该如何合并两个数据帧,以便生成的 dataframe 没有整列的 NaN 值? - How should I merge two dataframes so that the resulting dataframe does not have NaN values for an entire column? 如何在python中删除具有nan值的行 - How to remove rows with nan values in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM