简体   繁体   中英

How can I fill data frames with NAN with same values of previous data frames from the same list

I have a list of data frames A most of them are NAN data frames some of them are not, I would like to fill all NAN data frames with same values of the previous data frames (that do not contain NAN) in the list. Here's a small example:

A=[]

data = {'set_of_numbers': [1,2,3,4,4,5,9]}
df1 = pd.DataFrame(data,columns=['set_of_numbers'])
data2 = {'set_of_numbers': [0,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]}
df2 = pd.DataFrame(data2,columns=['set_of_numbers'])
data3 = {'set_of_numbers': [3,3,3,8,4,5,8]}
df3 = pd.DataFrame(data3,columns=['set_of_numbers'])
data4 = {'set_of_numbers': [0,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]}
df4 = pd.DataFrame(data4,columns=['set_of_numbers'])

A.append(df1)
A.append(df2)
A.append(df3)
A.append(df4)  
A  

I would like to have the output shown in the second picture, where all nan dataframes are filled with values of previous data frames

在此处输入图片说明

我想要这个输出:

If I understand correctly:

for i, df in enumerate(A):
  df[df.isnull()] = A[i-1]

or if you wish to change the dtype of previously non-nan df:

for i, df in enumerate(A):
  if df.isnull().all().all():
    A[i] = A[i-1].copy()

per OP's EDIT on question:

for i, df in enumerate(A):
  if df.isnull().any().any():
    A[i] = A[i-1].copy()

output:

[   set_of_numbers
0               1
1               2
2               3
3               4
4               4
5               5
6               9,    set_of_numbers
0               1
1               2
2               3
3               4
4               4
5               5
6               9,    set_of_numbers
0               3
1               3
2               3
3               8
4               4
5               5
6               8,    set_of_numbers
0               3
1               3
2               3
3               8
4               4
5               5
6               8]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM