如何从DataFram中获取非nan元素的索引和值

2024-09-19 23:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大数据框,有很多NaN,我想把它存储到一个较小的数据框中,这个数据框存储所有索引和非NaN、非零值的值。在

dff = pd.DataFrame(np.random.randn(4,3), columns=list('ABC'))
dff.iloc[0:2,0] = np.nan
dff.iloc[2,2] = np.nan
dff.iloc[1:4,1] = 0 

数据帧可能如下所示:

^{pr2}$

我想要这样的数据帧:

0   B  -2.268882
0   C  0.337074
1   C  1.340350
2   A  -1.526945
3   A  -1.223816
4   C  -2.185926

我怎么能很快做到呢,因为我有一个相对大的数据框,成千上千。。。在

非常感谢!在


Tags: columns数据dataframenprandomnanlistpd
2条回答

0替换为np.nan,并.stack()结果{a1}。在

如果有可能在.replace()之后的rows中有所有np.nan值,那么可以在.stack()之前做{},以减少要透视的行数。如果这适用于columnsdo`.dropna(how='all',axis=1)。在

df.replace(0, np.nan).stack()

0  B   -2.268882
   C    0.337074
1  C    1.340350
2  A   -1.526945
3  A   -1.223816
   C   -2.185926

根据需要与.reset_index()组合。在

要从带有MultiIndexSeries中选择,请使用.loc[(level_0, level_1)]

^{pr2}$

docs中有关切片等的详细信息。在

我想出了一个有点难看的方法来实现目标,但是嘿,这很管用。但是这个解决方案的索引是从0开始的,并且不会像您的问题一样保留“A”、“B”、“C”的原始顺序,如果这很重要的话。在

import pandas as pd
import numpy  as np
dff = pd.DataFrame(np.random.randn(4,3), columns=list('ABC'))
dff.iloc[0:2,0] = np.nan
dff.iloc[2,2] = np.nan
dff.iloc[1:4,1] = 0 
dff.iloc[2,1] = np.nan
​
# mask to do logical and for two lists
mask = lambda y,z: list(map(lambda x: x[0] and x[1], zip(y,z)))
# create new frame
new_df = pd.DataFrame()
types = []
vals  = []
# iterate over columns
for col in dff.columns:
    # get the non empty and non zero values from current column
        data = dff[col][mask(dff[col].notnull(), dff[col] != 0)]
        # add corresponding original column name 
        types.extend([col for x in range(len(data))])
        vals.extend(data)
        # populate the dataframe
new_df['Types'] = pd.Series(types)
new_df['Vals'] = pd.Series(vals)
​
print(new_df)
#          A         B         C
#0       NaN -1.167975 -1.362128
#1       NaN  0.000000  1.388611
#2  1.482621       NaN       NaN
#3 -1.108279  0.000000 -1.454491
#  Types      Vals
#0     A  1.482621
#1     A -1.108279
#2     B -1.167975
#3     C -1.362128
#4     C  1.388611
#5     C -1.454491

我期待更多的pandas/python类似的回答我自己!在

相关问题 更多 >