如何从pandas数据帧中提取列表或dict中的非NA值

时间:2021-12-07 22:55:50

I have a df like this,

我有这样的df,

df,

    AAA BBB CCC
0   4   10  100
1   5   20  50
2   6   30  -30
3   7   40  -50

df_mask = pd.DataFrame({'AAA' : [True] * 4, 'BBB' : [False] * 4,'CCC' : [True,False] * 2}) and df.where(df_mask) is

df_mask = pd.DataFrame({'AAA':[True] * 4,'BBB':[False] * 4,'CCC':[True,False] * 2})和df.where(df_mask)是

    AAA BBB CCC
0   4   NaN 100.0
1   5   NaN NaN
2   6   NaN -30.0
3   7   NaN NaN

I am trying to extract the non null values like this.

我试图像这样提取非空值。

I tried, df[df.where(df_mask).notnull()].to_dict() but it gives all the values

我试过,df [df.where(df_mask).notnull()]。to_dict()但它给出了所有的值

My expected output is,

我的预期产量是,

{'AAA': {0: 4, 1: 5, 2: 6, 3: 7},
 'CCC': {0: 100.0, 2: -30.0}}

2 个解决方案

#1


2  

Let's use agg here:

我们在这里使用agg:

v = df.where(df_mask).agg(lambda x: x.dropna().to_dict())

On older versions, apply does the same thing (albeit a bit slower).

在旧版本中,apply会做同样的事情(虽然有点慢)。

v = df.where(df_mask).apply(lambda x: x.dropna().to_dict())

And now, filter out rows with empty dictionaries for the final step:

现在,为最后一步过滤掉带有空字典的行:

res = v[v.str.len() > 0].to_dict()

print(res)
{'AAA': {0: 4.0, 1: 5.0, 2: 6.0, 3: 7.0}, 'CCC': {0: 100.0, 2: -30.0}}

Another apply-free option is a dict-comprehension:

另一个免费申请是字典理解:

v = df.where(df_mask)  
res = {k : v[k].dropna().to_dict() for k in df} 

print(res)
{'AAA': {0: 4, 1: 5, 2: 6, 3: 7}, 'BBB': {}, 'CCC': {0: 100.0, 2: -30.0}}

Note that this (slightly) simpler solution retains keys with empty values.

请注意,这个(稍微)更简单的解决方案会保留具有空值的键。

#2


1  

You can iterate df's columns and apply dropna Serieswise

您可以迭代df的列并应用dropna Serieswise

{col: df[col].dropna().values for col in df}

Which yields

{'AAA': array([4, 5, 6, 7]),
 'BBB': array([], dtype=float64),
 'CCC': array([ 100.,  -30.])}

You can filter out empty arrays such as 'BBB' with

您可以过滤掉空数组,例如'BBB'

{key: val for key, val in ddict.items() if val}

#1


2  

Let's use agg here:

我们在这里使用agg:

v = df.where(df_mask).agg(lambda x: x.dropna().to_dict())

On older versions, apply does the same thing (albeit a bit slower).

在旧版本中,apply会做同样的事情(虽然有点慢)。

v = df.where(df_mask).apply(lambda x: x.dropna().to_dict())

And now, filter out rows with empty dictionaries for the final step:

现在,为最后一步过滤掉带有空字典的行:

res = v[v.str.len() > 0].to_dict()

print(res)
{'AAA': {0: 4.0, 1: 5.0, 2: 6.0, 3: 7.0}, 'CCC': {0: 100.0, 2: -30.0}}

Another apply-free option is a dict-comprehension:

另一个免费申请是字典理解:

v = df.where(df_mask)  
res = {k : v[k].dropna().to_dict() for k in df} 

print(res)
{'AAA': {0: 4, 1: 5, 2: 6, 3: 7}, 'BBB': {}, 'CCC': {0: 100.0, 2: -30.0}}

Note that this (slightly) simpler solution retains keys with empty values.

请注意,这个(稍微)更简单的解决方案会保留具有空值的键。

#2


1  

You can iterate df's columns and apply dropna Serieswise

您可以迭代df的列并应用dropna Serieswise

{col: df[col].dropna().values for col in df}

Which yields

{'AAA': array([4, 5, 6, 7]),
 'BBB': array([], dtype=float64),
 'CCC': array([ 100.,  -30.])}

You can filter out empty arrays such as 'BBB' with

您可以过滤掉空数组,例如'BBB'

{key: val for key, val in ddict.items() if val}