我想根据给定条件获取行号和列号。 "坐标"如果你愿意的话。
import re
import pandas as pd
import numpy as np
dfp = pd.DataFrame({'A' : [1,21,8,44,np.NaN,6,75,8,44,999],
'B' : [1,1,3,5,0,0,np.NaN,9,0,0],
'C' : ['AA1233445','AA1233445', 'rmacy','Idaho Rx','Ab123455','TV192837','RX','Ohio Drugs','RX12345','USA Pharma'],
'D' : [123456,123456,1234567,12345678,12345,12345,12345678,123456789,1234567,np.NaN],
'E' : ['Assign','Assign','Hello','Ugly','Appreciate','Undo','Testing','Unicycle','Pharma','Unicorn',]})
print(dfp)
A B C D E
0 1.0 1.0 AA1233445 123456.0 Assign
1 21.0 1.0 AA1233445 123456.0 Assign
2 8.0 3.0 rmacy 1234567.0 Hello
3 44.0 5.0 Idaho Rx 12345678.0 Ugly
4 NaN 0.0 Ab123455 12345.0 Appreciate
5 6.0 0.0 TV192837 12345.0 Undo
6 75.0 NaN RX 12345678.0 Testing
7 8.0 9.0 Ohio Drugs 123456789.0 Unicycle
8 44.0 0.0 RX12345 1234567.0 Pharma
9 999.0 0.0 USA Pharma NaN Unicorn
我可以通过以下方式获得输出:
print(dfp.loc[dfp['B'].isnull()].index.values[0] + 1 ,
',', + int([i for i,x in enumerate(dfp.columns.tolist()) if x == 'B'][0] + 1))
但问题是如果B
有多个空值。我想要所有空值的坐标。
有没有办法使用dataframe.loc
或类似的东西来做到这一点?在值中添加1并不是什么大问题,我可以在以后轻松地做到这一点。
答案 0 :(得分:2)
我使用np.where
和zip
i, j = np.where(dfp.isnull().values)
# Coordinates in the space of the actual index and column names
list(zip(dfp.index[i], dfp.columns[j]))
[(4, 'A'), (6, 'B'), (9, 'D')]
否则,坚持
的序数位置list(zip(i, j))
[(4, 0), (6, 1), (9, 3)]
或者
np.column_stack([i, j])
array([[4, 0],
[6, 1],
[9, 3]])
答案 1 :(得分:2)
您可以使用
> final3pl
Call:
tpm(data = finalqnum, type = "latent.trait")
Coefficients:
Gussng Dffclt Dscrmn
p01 0.000 -1.503 1.379
p02 0.000 -1.407 1.427
p03 0.000 -1.121 1.873
p04 0.058 -0.888 2.428
p05 0.009 -1.049 2.345
p06 0.149 -1.010 2.158
p07 0.261 0.906 1.771
p09 0.000 -2.000 1.151
p10 0.000 -0.754 1.069
q31 0.021 -0.066 1.164
q10 0.205 0.261 1.028
q11 0.132 1.268 1.078
q12 0.077 -0.112 1.116
q13 0.170 2.847 2.245
q15 0.205 1.155 2.218
Log.Lik: -6954.86
为了简明地添加dfp[pd.isnull(dfp['B'])].index.tolist()
,您可以使用:
1
打印
np.asarray(dfp[pd.isnull(dfp['B'])].index) + 1
要包含B列的索引(print(np.asarray(dfp[pd.isnull(dfp['B'])].index) + 1)
):
dfp.columns.get_loc("B") + 1
在给定的列列表中查找“NaN”:
for x in np.asarray(dfp[pd.isnull(dfp['B'])].index) + 1:
print(str(x)+','+str(dfp.columns.get_loc("B") + 1))
一些解释
def find_NaN(list_col):
for c in list_col:
if c in dfp.columns:
for x in np.asarray(dfp[pd.isnull(dfp[c])].index) + 1:
print(str(x)+','+str(dfp.columns.get_loc(c) + 1))
find_NaN(["A","B"])
5,1
7,2
使用布尔值数组从数据框中选择数据。
dfp[pd.isnull(dfp['B'])]
给出了列dfp.columns.get_loc(c)
c