How to convert Pandas dataframe column into bin string data?
up vote
1
down vote
favorite
I have a Pandas dataframe called odf that looks like this:
Customer Employees
A 2
B 100
C 5
D 1000
I have created custom bins for the employee data:
df = odf['Employees']
bins = [0,5,1000]
df.value_counts(bins=bins)
(-0.001, 5.0] 2
(5.0, 1000] 2
Name:Employees, dtype: int64
now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:
Customer Employees NewBinColumn
A 2 -0.001, 5.0
B 100 5.0, 1000
C 5 -0.001, 5.0
D 1000 5.0, 1000
That way I can see the bin column next to the original dataframe columns
here is what I tried that did not work:
ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
ndf
And while it does join the two, what I get is this:
Customer EmployeesEmployees Employees
A 2 2
B 100 100
C 5 5
D 1000 1000
If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.
pandas dataframe join bins
add a comment |
up vote
1
down vote
favorite
I have a Pandas dataframe called odf that looks like this:
Customer Employees
A 2
B 100
C 5
D 1000
I have created custom bins for the employee data:
df = odf['Employees']
bins = [0,5,1000]
df.value_counts(bins=bins)
(-0.001, 5.0] 2
(5.0, 1000] 2
Name:Employees, dtype: int64
now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:
Customer Employees NewBinColumn
A 2 -0.001, 5.0
B 100 5.0, 1000
C 5 -0.001, 5.0
D 1000 5.0, 1000
That way I can see the bin column next to the original dataframe columns
here is what I tried that did not work:
ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
ndf
And while it does join the two, what I get is this:
Customer EmployeesEmployees Employees
A 2 2
B 100 100
C 5 5
D 1000 1000
If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.
pandas dataframe join bins
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a Pandas dataframe called odf that looks like this:
Customer Employees
A 2
B 100
C 5
D 1000
I have created custom bins for the employee data:
df = odf['Employees']
bins = [0,5,1000]
df.value_counts(bins=bins)
(-0.001, 5.0] 2
(5.0, 1000] 2
Name:Employees, dtype: int64
now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:
Customer Employees NewBinColumn
A 2 -0.001, 5.0
B 100 5.0, 1000
C 5 -0.001, 5.0
D 1000 5.0, 1000
That way I can see the bin column next to the original dataframe columns
here is what I tried that did not work:
ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
ndf
And while it does join the two, what I get is this:
Customer EmployeesEmployees Employees
A 2 2
B 100 100
C 5 5
D 1000 1000
If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.
pandas dataframe join bins
I have a Pandas dataframe called odf that looks like this:
Customer Employees
A 2
B 100
C 5
D 1000
I have created custom bins for the employee data:
df = odf['Employees']
bins = [0,5,1000]
df.value_counts(bins=bins)
(-0.001, 5.0] 2
(5.0, 1000] 2
Name:Employees, dtype: int64
now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:
Customer Employees NewBinColumn
A 2 -0.001, 5.0
B 100 5.0, 1000
C 5 -0.001, 5.0
D 1000 5.0, 1000
That way I can see the bin column next to the original dataframe columns
here is what I tried that did not work:
ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
ndf
And while it does join the two, what I get is this:
Customer EmployeesEmployees Employees
A 2 2
B 100 100
C 5 5
D 1000 1000
If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.
pandas dataframe join bins
pandas dataframe join bins
asked Nov 9 at 19:45
user76595
728
728
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
It is not exactly the same formating that what you want, but using pd.cut
on odf['Employees']
such as:
odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)
will give:
Customer Employees NewBinColumn
0 A 2 (0, 5]
1 B 100 (5, 1000]
2 C 5 (0, 5]
3 D 1000 (5, 1000]
1
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It is not exactly the same formating that what you want, but using pd.cut
on odf['Employees']
such as:
odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)
will give:
Customer Employees NewBinColumn
0 A 2 (0, 5]
1 B 100 (5, 1000]
2 C 5 (0, 5]
3 D 1000 (5, 1000]
1
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
add a comment |
up vote
1
down vote
accepted
It is not exactly the same formating that what you want, but using pd.cut
on odf['Employees']
such as:
odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)
will give:
Customer Employees NewBinColumn
0 A 2 (0, 5]
1 B 100 (5, 1000]
2 C 5 (0, 5]
3 D 1000 (5, 1000]
1
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It is not exactly the same formating that what you want, but using pd.cut
on odf['Employees']
such as:
odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)
will give:
Customer Employees NewBinColumn
0 A 2 (0, 5]
1 B 100 (5, 1000]
2 C 5 (0, 5]
3 D 1000 (5, 1000]
It is not exactly the same formating that what you want, but using pd.cut
on odf['Employees']
such as:
odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)
will give:
Customer Employees NewBinColumn
0 A 2 (0, 5]
1 B 100 (5, 1000]
2 C 5 (0, 5]
3 D 1000 (5, 1000]
answered Nov 9 at 19:58
Ben.T
4,8072523
4,8072523
1
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
add a comment |
1
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
1
1
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
– user76595
Nov 9 at 20:07
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232347%2fhow-to-convert-pandas-dataframe-column-into-bin-string-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown