I have a CSV file like 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 to have a single data frame with just one time "line".
The idea was to create two data frames and merge them to one with resp to column Time [s]. So I created that 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 it did not work. KeyError: 'Time [s]'
/**************************************************************************/
I figured out that pandas is adding a numbering to the columns that are duplicated. So I changed my code like 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 now I have the issue that the index is just sorted for elements that have no NaN. Means first all rows where both columns have Numbers, then where just the first column has no NaN and then where just the 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 it in this 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
... ...