rod.hoda
rod.hoda

Reputation: 61

How to slice different parts of each element in a Pandas Series?

I have a Series that looks like this:

0     15022$33.24%15.0k
1       15022$9.31%4.2k
2       15022$8.19%3.7k
3       15022$8.17%3.7k
4       15022$7.57%3.4k
5       5746$12.71%5.7k
6       5083$11.25%5.1k
7       5027$11.12%5.0k
8        5027$6.13%2.8k
9        5027$4.99%2.3k
10      4601$10.18%4.6k
11       3771$8.34%3.8k
12       3089$6.83%3.1k
13       3089$4.95%2.2k
14      3089$1.88%850.0
15       1673$3.70%1.7k
16       1184$2.62%1.2k
17           1$0.00%1.0
Name: Amount, dtype: object

This first number before the dollar sign is the whole number value, then it is the percentage, and finally the abbreviated form of the same value.

I am trying to put this Series into a DataFrame with each value having its own column (whole number value, percent, and abbreviated form).

This is the sample solution of the DataFrame using google sheets:

0   Whole   Percent Abbreviated num.
1   15022   33.24   15.0k
2   15022   9.31    4.2k
3   15022   8.19    3.7k
4   15022   8.17    3.7k
5   15022   7.57    3.4k

Thanks in advance!

Upvotes: 2

Views: 134

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133508

Could you please try following, based on your shown samples only.

import pandas as pd
import numpy as np


df=pd.Series(data=['15022$33.24%15.0k','15022$9.31%4.2k','15022$8.19%3.7k','15022$8.17%3.7k'], name='Amounts')
df=df.str.split('\$|%', expand=True)
df.columns = ["Whole","Percent","Abbreviated num."]
df.insert(0, "0", df.index+1, True) 
df

Upvotes: 3

Doug F
Doug F

Reputation: 167

Not sure if this is ideal, but apply a split to each row

s = pd.Series(index=[1,2,3],data=['15022$33.24%15.0k','15022$9.31%4.2k','15022$8.19%3.7k'], name='Amounts')
s.str.split('\$|%', expand=True)

you get

       0      1      2
1  15022  33.24  15.0k
2  15022   9.31   4.2k
3  15022   8.19   3.7k

Its a dataframe, rename the columns.

Upvotes: 3

Related Questions