Home > Mobile >  How to divide the cell into multiple cells?
How to divide the cell into multiple cells?

Time:06-29

I have one csv file and first and third column we have date format as 1219376117 like first four digits is days(xxxx), hours(yy), minutes(zz), second(ss) as xxxxyyzzss respectively. Everything is in one cell, so how can I separate it?

import numpy as np
import math
import pandas as pd

df = pd.read_csv ('SensorsAP_example.csv')
print (df)


## Splitting the data sets
#df = pd.DataFrame({"Time stamp":["unchange"],"time_tow":["Days, hours, minutes, seconds"]}) 

CodePudding user response:

One of the way is to first try and apply this to a simple string, which you can extend. E.g

s="1219376117"
days,hours,minutes,seconds=s[:4],s[4:6],s[6:8],s[8:]

Post that you can leverage apply function:

import pandas as pd
data={'timestamp':["1219376117","1219376118"]}
df=pd.DataFrame(data)

def split_timestamp(s):
   days,hours,minutes,seconds=s[:4],s[4:6],s[6:8],s[8:]
   return pd.Series([days,hours,minutes,seconds])
df[["days","hours","minutes","seconds"]]=df['timestamp'].apply(split_timestamp)

Output:

In [15]: df
Out[15]:
    timestamp  days hours minutes seconds
0  1219376117  1219    37      61      17
1  1219376118  1219    37      61      18
  • Related