Home > Enterprise >  pandas replace "/n" and take the balance data to new cell
pandas replace "/n" and take the balance data to new cell

Time:10-10

I am having following data in text file.

607 1800.00
608  1%
609 0.0
610 0\n09:20:00

I want "/n" to go away and the 09:22:03 should come in the new line of 611. Thanks.

CodePudding user response:

Assuming the literal \n two characters in your file, you can split on it, then explode to new rows and reincrement the first column:

(pd.read_csv('input.csv', sep='\s ', header=None, names=['A', 'B'], dtype={'B': 'string'})
   .assign(B=lambda d: d['B'].str.split(r'\\n'))
   .explode('B')
   .assign(A=lambda d: d['A'] d['A'].diff().eq(0).cumsum())
   .to_csv('out.csv', sep=' ', index=False, header=None)
)

Output file:

607 1800.00
608  1%
609 0.0
610 0
611 09:20:00
  • Related