Reputation: 41
I've DataFrame like this:
5 1 89107633648 19239.9
6 nan 9348.62
7 nan 3191.83
8 nan 2962.06
9 nan 3737.39
10 2 9156559561 6318.99
11 nan 2138.27
12 nan 1933.3
13 nan 2247.42
14 3 9107812806 25324.19
15 nan 1169.1
16 nan 3750.52
17 nan 3436.4
18 nan 5140.16
19 nan nan
20 nan 3196.79
21 nan 2985.62
22 nan 2869.01
23 nan 2776.59
I need to get a two-dimensional list grouped by nan field.
[[9348.62, 3191.83, 2962.06, 3737.39], [2138.27, 1933.3, 2247.42], ...]
How can I do that?
n_rows = res.astype(bool).sum(axis=0)['Unnamed: 5']
rows_3 = [[] for i in range(n_rows)]
for i in range(0, len(res)):
tmp = 0
j =0
if res.iloc[i]['Unnamed: 5'] is '':
tmp = res.iloc[i][12]
else:
j += 1
rows_3[j].append(tmp)
tmp =0
print(rows_3)
Upvotes: 0
Views: 106
Reputation: 8302
Here is solution, using Series.fillna
with ffill
then GroupBy
followed by list splice.
# renamed columns for convenience
df.columns = ['col1', 'col2', 'col3', 'col4']
print(
df.assign(col2 = df.col2.fillna(method='ffill'))
.groupby('col2')['col3'].agg(lambda x: x.tolist()[1:])
.reset_index(drop=True).tolist()
)
Upvotes: 1