I have an object, slist
that I need to split, reformat the date, and export as a tab delimited file. For the splitting I think I’m tripping up understanding the first row? Here is slist
:
I’ve tried the following:
JavaScript
x
3
1
df = pd.DataFrame(data=slist)
2
newdf['datetime','values'] = df['node_21_Depth_above_invert'].astype(str).str.split(' ',expand=True)
3
Which gives me something like this:
I’ve spent a ton of time trying to figure this out and I know there are a lot of other pandas questions about column splitting, but I’ve hit a wall and any insight would be helpful. Thanks!
Advertisement
Answer
As you now have the datetime as row index, you can make it a data column by .reset_index()
and then rename the columns, as follows:
JavaScript
1
3
1
newdf = df.reset_index()
2
newdf.columns = ['datetime','values']
3
Test Data Preparation
JavaScript
1
13
13
1
slist = {'node_21_Depth_above_invert': {pd.Timestamp('1998-01-01 01:00:00'): 1.0, pd.Timestamp('1998-01-01 02:00:00'): 1.519419550895691, pd.Timestamp('1998-01-01 03:00:00'): 2.0, pd.Timestamp('1998-01-01 04:00:00'): 2.0, pd.Timestamp('1998-01-01 05:00:00'): 2.0}}
2
3
df = pd.DataFrame(data=slist)
4
5
print(df)
6
7
node_21_Depth_above_invert
8
1998-01-01 01:00:00 1.00000
9
1998-01-01 02:00:00 1.51942
10
1998-01-01 03:00:00 2.00000
11
1998-01-01 04:00:00 2.00000
12
1998-01-01 05:00:00 2.00000
13
Run New Codes
JavaScript
1
3
1
newdf = df.reset_index()
2
newdf.columns = ['datetime','values']
3
Result:
JavaScript
1
9
1
print(newdf)
2
3
datetime values
4
0 1998-01-01 01:00:00 1.00000
5
1 1998-01-01 02:00:00 1.51942
6
2 1998-01-01 03:00:00 2.00000
7
3 1998-01-01 04:00:00 2.00000
8
4 1998-01-01 05:00:00 2.00000
9