Get a specific number of rows and place in new column pandas
You can try something like that (please notice the path format):
def split_df(df, k_rows, path):
count = 0
df_index = 0
while count <= len(df.index):
if count == 0:
new_df = df.iloc[:k_rows, :]
new_df.to_csv(path+str(df_index)+".csv")
else:
new_df = df.iloc[count:count+k_rows, :]
new_df.to_csv(path+str(df_index)+".csv")
count += k_rows
df_index += 1
split_df(df_test, 3, "./split_csv/df_")
Edit - You can try this:
def split_df(df, k_rows, path):
list_df = []
count = 0
while count <= len(df.index):
if count == 0:
new_df = df.iloc[:k_rows, :]
list_df.append(new_df)
else:
new_df = df.iloc[count:count+k_rows, :]
list_df.append(new_df)
count += k_rows
final_df = pd.concat(list_df, axis=1)
for col in final_df.columns:
final_df = final_df.replace(col, np.nan).apply(lambda x: pd.Series(x.dropna().to_numpy()))
final_df.to_csv(path)
split_df(df_test, 3, "./split_csv/final_df.csv")