[英]How to print excel column non null value with python?
I have the following excel sheet:我有以下 excel 表:
and want to print column 1 value if the column 2 value is not null. The output should be [1,3].如果第 2 列的值不是 null,则想要打印第 1 列的值。output 应该是 [1,3]。
This the script created by me, but it doesn't work:这是我创建的脚本,但它不起作用:
import xlrd
import pandas as pd
filename='test.xlsx'
dataframe = pd.read_excel(filename)
frame = dataframe.loc[dataframe["col2"] !=" "]
df = frame.iloc[:, 0]
ndarray = df.to_numpy()
print(ndarray)
You can first filter down to nona rows and then show the values of the column you want to show:您可以先过滤到 nona 行,然后显示要显示的列的值:
dataframe[df['col2'].notna()]['col1'].values
If you print the dataframe, you will see that the empty cells are NaN
:如果打印 dataframe,您将看到空单元格为NaN
:
Col1 Col2
0 1 a
1 2 NaN
2 3 b
3 4 NaN
So, you need to use the notna()
method to filter所以,需要使用notna()
方法进行过滤
Here is your fixed code:这是您的固定代码:
import xlrd
import pandas as pd
filename='test.xlsx'
dataframe = pd.read_excel(filename)
frame = dataframe.loc[dataframe["col2"].notna()]
df = frame.iloc[:, 0]
ndarray = df.to_numpy()
print(ndarray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.