Reputation: 5107
I have a df which looks like:
BBG.LON.123.S_CAR_ADJ_DPS 343.94325
BBG.LON.436.S_CAR_ADJ_DPS 236.51530
I am trying to rename the row names (removing the '_CAR_ADJ_DPS' element of each row name and rename the column 'id' so my resulting df looks like:
id
BBG.LON.123.S 343.94325
BBG.LON.436.S 236.51530
I have tried using the following line without success:
pd.DataFrame(pd.Series(np.unique([row.split('_')[0] for row in df.rows]), name='id'))
What can I try next?
Upvotes: 4
Views: 4581
Reputation: 375545
You may also be interested in str.extract to pull out the entries as columns:
In [11]: df[0].str.extract('(?P<A>.*)\.(?P<B>.*)\.(?P<C>\d+)\.(?P<D>.)_.*', expand=True)
Out[11]:
A B C D
0 BBG LON 123 S
1 BBG LON 436 S
Upvotes: 2
Reputation: 862761
I think you can use str.split
with rename_axis
(new in pandas
0.18.0
):
print (df)
a
BBG.LON.123.S_CAR_ADJ_DPS 343.94325
BBG.LON.436.S_CAR_ADJ_DPS 236.51530
df.index = df.index.str.split('_').str[0]
df = df.rename_axis('id')
#if use pandas bellow 0.18.0
#df.index.name = 'id'
print (df)
a
id
BBG.LON.123.S 343.94325
BBG.LON.436.S 236.51530
Upvotes: 3