You can use read_csv
and specify header=None
and pass the column names as a list:
In [124]:
t="""time1,stockA,bid,1
time2,stockA,ask,1.1
time3,stockB,ask,2.1
time4,stockB,bid,2.0"""
df = pd.read_csv(io.StringIO(t), header=None, names=['time', 'stock', 'bid', 'ask'])
df
Out[124]:
time stock bid ask
0 time1 stockA bid 1.0
1 time2 stockA ask 1.1
2 time3 stockB ask 2.1
3 time4 stockB bid 2.0
You'll have to re-encode the bid column to 1 or 2:
In [126]:
df['bid'] = df['bid'].replace('bid', 1)
df['bid'] = df['bid'].replace('ask', 2)
df
Out[126]:
time stock bid ask
0 time1 stockA 1 1.0
1 time2 stockA 2 1.1
2 time3 stockB 2 2.1
3 time4 stockB 1 2.0
EDIT
Based on your updated sample data and desired output the following works:
In [29]:
t="""time1,stockA,bid,1
time2,stockA,ask,1.1
time3,stockB,ask,2.1
time4,stockB,bid,2.0
time5,stockA,bid,1.1
time6,stockA,ask,1.2"""
df = pd.read_csv(io.StringIO(t), header=None, names=['time', 'stock', 'bid', 'ask'])
df
Out[29]:
time stock bid ask
0 time1 stockA bid 1.0
1 time2 stockA ask 1.1
2 time3 stockB ask 2.1
3 time4 stockB bid 2.0
4 time5 stockA bid 1.1
5 time6 stockA ask 1.2
In [30]:
df.loc[df['bid'] == 'bid', 'bid'] = df['ask']
df.loc[df['bid'] != 'ask', 'ask'] = ''
df.loc[df['bid'] == 'ask','bid'] = ''
df
Out[30]:
time stock bid ask
0 time1 stockA 1
1 time2 stockA 1.1
2 time3 stockB 2.1
3 time4 stockB 2
4 time5 stockA 1.1
5 time6 stockA 1.2