473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Number and Character Formats

Hi There,

I need your advice on formatting.

Dataset below:

Expand|Select|Wrap|Line Numbers
  1. data = {'funds': ['DOMCSP','DOMCSP','DOMFEE','DOMFEE','INTON','INTON','INTOFF','INTOFF'], 
  2.          'risk': [1, 2, 3, 4, 5,6,7,100]}
  3. df = pd.DataFrame(data, columns = ['funds', 'risk',])
  4. df
  5.  
If funds are DOMCSP and DOMFEE then format it as Domestic if funds are INTON and INTOFF then format it as International.

If risk 1-3 then format it as Low
if risk 4-5 then format it as Medium
if risk >=6 then format it as High

Looking for your advice.

Kind regards,
CK
Feb 28 '20 #1

✓ answered by SioSio

It can replace String Type with String Type, but it can't replace Integer Type ​​with String Type (strictly possible, but not recommended).

The code below removes the "risk" from the dataframe and adds a new "risk" dataframe.

Expand|Select|Wrap|Line Numbers
  1. df.replace('DOMCSP', 'Domestic', inplace=True)
  2. df.replace('DOMFEE', 'Domestic', inplace=True)
  3. df.replace('INTON', 'International', inplace=True)
  4. df.replace('INTOFF', 'International', inplace=True)
  5. risk=[]
  6. for j in df["risk"]:
  7.     if j >= 1 and j <= 3:
  8.         risk.append('Low')
  9.     elif j >= 4 and j <= 5:
  10.         risk.append('Medium')
  11.     elif j >= 6:
  12.         risk.append('High')
  13.     else:
  14.         risk.append('')
  15. df.drop('risk', axis=1)
  16. df ['risk'] = risk

9 2186
SioSio
272 256MB
I don't know what "FORMAT" means, but in the following example add it to df as format1 and format2.
Expand|Select|Wrap|Line Numbers
  1. data = {'funds': ['DOMCSP','DOMCSP','DOMFEE','DOMFEE','INTON','INTON','INTOFF','INTOFF'],
  2.          'risk': [1, 2, 3, 4, 5,6,7,100]}
  3. df = pd.DataFrame(data, columns = ['funds', 'risk',])
  4. df
  5.  
  6. format1=[]
  7. format2=[]
  8. for i in df["funds"]:
  9.     if i == 'DOMCSP' or i == 'DOMFEE':
  10.         format1.append('Domestic')
  11.     elif i == 'INTON' or i == 'INTOFF':
  12.         format1.append('International')
  13. for j in df["risk"]:
  14.     if j >= 1 and j <= 3:
  15.         format2.append('Low')
  16.     elif j >= 4 and j <= 5:
  17.         format2.append('Medium')
  18.     elif j >= 6:
  19.         format2.append('High')
  20. df ["format1"] = format1
  21. df ["format2"] = format2
  22. print(df)
  23.  
Feb 28 '20 #2
HI There,

Sorry, we use that term format in SAS a lot.

I am talking something like this below
Expand|Select|Wrap|Line Numbers
  1. broad_format={"DOMCSP":"Domestic Common Wealth Supported","DOMFEE":"Domestic Fee Paying",
  2.              "DOMRTS":"Domestic Research Training Scheme",
  3.            "INTOFF":"International Offshore","INTON":"International"}
  4.  
  5. crse_type_format={1:"Higher Doctorate",2:"Doctorate by Research",10:"Bachelor's Pass",
  6.                 11:"Graduate Certificate",12:"Doctorate by Coursework"}
Expand|Select|Wrap|Line Numbers
  1. enrol_v1.replace({"broad_funds":broad_format,"course_type":crse_type_format})
Is it something which can be done just like above.
Feb 28 '20 #3
SioSio
272 256MB
Will it create a new DataFrame "enrol_v1" from "df" and the conditions?

Expand|Select|Wrap|Line Numbers
  1. data = {'funds': ['DOMCSP','DOMCSP','DOMFEE','DOMFEE','INTON','INTON','INTOFF','INTOFF'],
  2.          'risk': [1, 2, 3, 4, 5,6,7,100]}
  3. df = pd.DataFrame(data, columns = ['funds', 'risk',])
  4. df
  5. enrol_v1 = pd.DataFrame(columns = ['broad_funds','broad_format','course_type','crse_type_format'])
  6.  
  7. broad_format=[]
  8. crse_type_format=[]
  9. for i in df["funds"]:
  10.     if i == 'DOMCSP' or i == 'DOMFEE':
  11.         broad_format.append('Domestic')
  12.     elif i == 'INTON' or i == 'INTOFF':
  13.         broad_format.append('International')
  14.     else:
  15.         broad_format.append('')
  16. for j in df["risk"]:
  17.     if j >= 1 and j <= 3:
  18.         crse_type_format.append('Low')
  19.     elif j >= 4 and j <= 5:
  20.         crse_type_format.append('Medium')
  21.     elif j >= 6:
  22.         crse_type_format.append('High')
  23.     else:
  24.         crse_type_format.append('')
  25.  
  26. enrol_v1 ['broad_funds'] =   df['funds']
  27. enrol_v1 ['broad_format'] = broad_format
  28. enrol_v1 ['course_type'] =   df['risk']
  29. enrol_v1 ['crse_type_format'] = crse_type_format
  30. print(enrol_v1)
Feb 28 '20 #4
Hi There,
Sorry for the confusion.

I don't want to create another data frame or the condition:
what I meant was similar to the recent post I want to use REPLACE function.
There was a post in StackOverflow using Apply.
https://stackoverflow.com/questions/...ting-in-python
I am able to do it for a single observation, but when it comes to multiple observations I am not able to.
Feb 28 '20 #5
Hi,
Just to give you a heads up.
This is the format I have created in SAS. and the format name is Broad Funds, I can use this format Broad Funds anytime I want in my SAS coding. Its just one-time formatting and calling that format whenever I required in my coding.
Expand|Select|Wrap|Line Numbers
  1. Value $Broad_Funds
  2. 'DOMCSP','DOMRTS','DOMRTP'= " Domestic C'wealth Sup"
  3. 'DOMFEE'="Domestic Fee-Paying"
  4. 'INTON','INTRTP' =" International On-shore"
  5. 'INTOFF'="International Off-shore";
Instead of a short name, I have created SAS format Broad_Funds which is an abbreviation.
Let me know if this helps you to understand my requirement.
Feb 28 '20 #6
SioSio
272 256MB
It can replace String Type with String Type, but it can't replace Integer Type ​​with String Type (strictly possible, but not recommended).

The code below removes the "risk" from the dataframe and adds a new "risk" dataframe.

Expand|Select|Wrap|Line Numbers
  1. df.replace('DOMCSP', 'Domestic', inplace=True)
  2. df.replace('DOMFEE', 'Domestic', inplace=True)
  3. df.replace('INTON', 'International', inplace=True)
  4. df.replace('INTOFF', 'International', inplace=True)
  5. risk=[]
  6. for j in df["risk"]:
  7.     if j >= 1 and j <= 3:
  8.         risk.append('Low')
  9.     elif j >= 4 and j <= 5:
  10.         risk.append('Medium')
  11.     elif j >= 6:
  12.         risk.append('High')
  13.     else:
  14.         risk.append('')
  15. df.drop('risk', axis=1)
  16. df ['risk'] = risk
Mar 2 '20 #7
Hi There,
Thanks for the advice.
I will make a note of it.
Mar 2 '20 #8
SioSio
272 256MB
For reference,
It also shows how direct replacement code uses "slices".
But, this will put a warning message.
Expand|Select|Wrap|Line Numbers
  1. l = 0
  2. for k in df['risk']:
  3.     if k >= 1 and k <= 3:
  4.         df['risk'][l] = 'Low'
  5.     elif k >= 4 and k <= 5:
  6.         df['risk'][l] = 'Medium'
  7.     elif k >= 6:
  8.         df['risk'][l] = 'High'
  9.     l = l + 1
Mar 2 '20 #9
Hi There,
Thanks for this.
Mar 3 '20 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Raquel | last post by:
Could someone explain to me what the reason is for having a character delimiter (which is double quotes by default) for performing Loads/Imports on UDB? I would think that column delimiter along...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
4
by: Vig | last post by:
Is scanf or any other function capable of reading numbers in the format 1.2345d-13 where 'd' serves the same role as 'e' usually does in scientific notation? This operation is iterated through...
16
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
4
by: Newsgroups | last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function is?
9
by: Velvet | last post by:
I'm trying to convert some JavaScript to C# and don't know how to get the character code of a character in a string. in JavaScript it is as follows: for( i = 0; i < email.length; i++) { ...
0
by: buu | last post by:
How could I replace all characters from string wich are not characters, numbers or some punctuation? note that I would not like to use any kind of loops is there any simple way?
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
4
by: luke noob | last post by:
This is my HTML... <head> <script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script> <script type="text/javascript" src="js/script.js"></script> </head> <body>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.