Structuring a 2D array from a pandas dataframe
IIUC, you can do something like:
n=2 #change this as you like for no. of splits
s=df.Text.dropna().reset_index(drop=True)
c=s.groupby(s.index//n).cumcount().eq(0).shift().shift(-1).fillna(False)
[[i] for i in s.groupby(c.cumsum()).apply(' '.join).tolist()]
[['Hi this is'], ['just a'], ['single sentence.'],
['This is another one.'], ['This is a'], ['third sentence .']]
EDIT:
d=dict(zip(df.loc[df.Text.notna(),'Text'].index,c.index))
ser=pd.Series(d)
df['new']=ser.reindex(range(ser.index.min(),
ser.index.max()+1)).map(c).fillna(False).astype(int)
print(df)
Text Selection_Values new
0 Hi 0 1
1 this is 0 0
2 just 0 1
3 a 0 0
4 single 0 1
5 sentence. 1 0
6 This 0 1
7 NaN 0 0
8 is another one. 1 0
9 This is 0 1
10 a 0 0
11 third 0 1
12 sentence 0 0
13 . 0 0
Here is a rather long, and bulky code, but it does the job! :)
selection_values = df["Selection_Values"].tolist()
max_number_of_cells_per_list = 3
a = [[s.str.cat(sep=' ')] for s in np.split(df.Text, df[df.Selection_Values == 1].index+1) if not s.empty]
print(a)
number_of_cells = 0
j = 0
for i in range(len(df['Text'])):
if isinstance(df['Text'][i], str):
number_of_cells += 1
if df["Selection_Values"][i] == 1 or i == len(df['Text'])-1:
print("j: ", j)
if number_of_cells > max_number_of_cells_per_list:
print(number_of_cells,max_number_of_cells_per_list)
print("\nmax number of cells reached")
n = np.ceil(np.divide(number_of_cells,max_number_of_cells_per_list))
print("deviding into ", n, " cells")
add = int((i-j)/n)
print("add", add)
for k in range(int(n)):
if k == n-1:
j = i
else:
j += add
print("j: ", j)
selection_values[j] = 1
print("\n")
# Reset Cell Counter Every time a new list should start
number_of_cells = 0
j = i
df['Selection_Values'] = selection_values
print("\n", df)
a = [[s.str.cat(sep=' ')] for s in np.split(df.Text, df[df.Selection_Values == 1].index+1) if not s.empty]
print(a)
You get:
Text Selection_Values
0 Hi 0
1 this is 0
2 just 0
3 a 0
4 single 0
5 sentence. 1
6 This 0
7 NaN 0
8 is another one. 1
9 This is 0
10 a 0
11 third 0
12 sentence 0
13 . 0
[['Hi this is just a single sentence.'], ['This is another one.'], ['This is a third sentence .']]
j: 0
6 3
max number of cells reached
deviding into 2.0 cells
add 2
j: 2
j: 5
j: 5
j: 8
5 3
max number of cells reached
deviding into 2.0 cells
add 2
j: 10
j: 13
Text Selection_Values
0 Hi 0
1 this is 0
2 just 1
3 a 0
4 single 0
5 sentence. 1
6 This 0
7 NaN 0
8 is another one. 1
9 This is 0
10 a 1
11 third 0
12 sentence 0
13 . 1
[['Hi this is just'], ['a single sentence.'], ['This is another one.'], ['This is a'], ['third sentence .']]