python - How to get two dataframes from one csv with multiple index columns -


i have csv file that:

time [s],channel 0-analog, time [s],reset-digital, time [s],channel 1-digital, time [s],channel 2-digital, time [s],channel 3-digital -0.002204166666667, 2048.000000000000000, -0.002204166666667, 1, -0.002204166666667, 0, -0.002204166666667, 1, -0.002204166666667, 1 -0.002204000000000, 2048.000000000000000, -0.001124000000000, 0, -0.001504666666667, 1, -0.001448500000000, 0, -0.000199666666667, 0 -0.002203833333333, 2048.000000000000000, -0.000000000000000, 1, 0.000301666666667, 0, 0.000841666666667, 1, 0.000056333333333, 1 -0.002203666666667, 2048.000000000000000, 0.000550833333333, 0, 0.000932000000000, 1, 0.003178666666667, 0, 0.002361000000000, 0 -0.002203500000000, 2048.000000000000000, 0.003259333333333, 1, 0.002538166666667, 0, 0.005142333333333, 1, 0.004062000000000, 1 -0.002203333333333, 2048.000000000000000, 0.005602833333333, 0, ... 

and want have single data frame 1 time "line".

the idea create 2 data frames , merge them 1 resp column time [s]. created sequence.

df1 = pd.read_csv('untitled.csv',usecols=[2,3]) df2 = pd.read_csv('untitled.csv',usecols=[4,5])  merged = pd.merge(df1,df2,on=r'time [s]') 

but did not work. keyerror: 'time [s]'

/**************************************************************************/

i figured out pandas adding numbering columns duplicated. changed code this.

df1 = pd.read_csv('untitled.csv',usecols=[2,3]) df2 = pd.read_csv('untitled.csv',usecols=[4,5]) df1.columns = df1.columns.str.strip('.123 ') df2.columns = df2.columns.str.strip('.123 ') merged =pd.merge(df1,df2,on=r'time [s]',how='outer') merged.set_index(r'time [s]') 

but have issue index sorted elements have no nan. means first rows both columns have numbers, first column has no nan , second column has no nan.

           reset-digital  channel 1-digital    time [s]                                       -0.002204            1.0                0.0    -0.001124            0.0                nan    -0.000000            1.0                nan     0.000551            0.0                nan               ...                  ...    -0.001505            nan                1.0     0.000302            nan                0.0     0.000932            nan                1.0     0.002538            nan                0.0               ...                    ... 

i need in format

           reset-digital  channel 1-digital    time [s]                                        0.000302            nan                0.0     0.000551            0.0                nan     0.000932            nan                1.0     0.002538            nan                0.0    -0.000000            1.0                nan    -0.001124            0.0                nan    -0.001505            nan                1.0    -0.002204            1.0                0.0               ...                    ... 

solution works if columns names unique , time columns previous cols of signal columns:

#get columns digital text d = df.columns[df.columns.str.contains('digital')] print (d) index(['reset-digital', 'channel 1-digital', 'channel 2-digital',        'channel 3-digital'],       dtype='object')  #get previous columns (time columns) #for new versions of pandas time columns added 1,2..for no duplicates td = df.columns[df.columns.get_indexer(d) - 1] print(td) index(['time [s].1', 'time [s].2', 'time [s].3', 'time [s].4'], dtype='object') 

#zip time , signal column , concat data df = pd.concat([df.set_index(x[0])[x[1]] x in zip(td, d)], axis=1) print (df)           reset-digital  channel 1-digital  channel 2-digital  \ -0.002204            1.0                0.0                1.0    -0.001505            nan                1.0                nan    -0.001448            nan                nan                0.0    -0.001124            0.0                nan                nan    -0.000200            nan                nan                nan    -0.000000            1.0                nan                nan     0.000056            nan                nan                nan     0.000302            nan                0.0                nan     0.000551            0.0                nan                nan     0.000842            nan                nan                1.0     0.000932            nan                1.0                nan     0.002361            nan                nan                nan     0.002538            nan                0.0                nan     0.003179            nan                nan                0.0     0.003259            1.0                nan                nan     0.004062            nan                nan                nan     0.005142            nan                nan                1.0                channel 3-digital   -0.002204                1.0   -0.001505                nan   -0.001448                nan   -0.001124                nan   -0.000200                0.0   -0.000000                nan    0.000056                1.0    0.000302                nan    0.000551                nan    0.000842                nan    0.000932                nan    0.002361                0.0    0.002538                nan    0.003179                nan    0.003259                nan    0.004062                1.0    0.005142                nan   

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -