My text file contain data of temp variation
#
1 2
2 4
3 4
#
6 1
3 2
1 7
I want the column values to be splitted at # and generate the new files by appending the splitted files
expected output1
1 6
2 3
3 1
expected output2
2 1
4 2
4 7
CodePudding user response:
A more complex problem than it seems at first glance. I had to carefully look at the example output to fully see what was going on.
Simulated text file:
sim_txt = io.StringIO('''
#
1 2
2 4
3 4
#
6 1
3 2
1 7
''')
df = pd.read_csv(sim_txt, sep='\s ', header=None, names=[0,1])
df_out = df.assign(out=df[0].str.contains('#').cumsum()) \
.pivot(columns=['out']).apply(lambda x: x.shift(-len(x)//2) if x.name[1]==2 else x) \
.dropna().astype(int)
print(df_out)
0 1
out 1 2 1 2
1 1 6 2 1
2 2 3 4 2
3 3 1 4 7
Then save to individual files:
for c in df_out.columns.get_level_values(0).unique():
df_out.loc[1:,c].to_csv(fr'd:\jchtempnew\SO\output{c 1}.txt', index=None, header=None, sep=' ')
output1
1 6
2 3
3 1
output2
2 1
4 2
4 7