Hi There,
I need your advice on formatting.
Dataset below: - data = {'funds': ['DOMCSP','DOMCSP','DOMFEE','DOMFEE','INTON','INTON','INTOFF','INTOFF'],
-
'risk': [1, 2, 3, 4, 5,6,7,100]}
-
df = pd.DataFrame(data, columns = ['funds', 'risk',])
-
df
-
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
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. - df.replace('DOMCSP', 'Domestic', inplace=True)
-
df.replace('DOMFEE', 'Domestic', inplace=True)
-
df.replace('INTON', 'International', inplace=True)
-
df.replace('INTOFF', 'International', inplace=True)
-
risk=[]
-
for j in df["risk"]:
-
if j >= 1 and j <= 3:
-
risk.append('Low')
-
elif j >= 4 and j <= 5:
-
risk.append('Medium')
-
elif j >= 6:
-
risk.append('High')
-
else:
-
risk.append('')
-
df.drop('risk', axis=1)
-
df ['risk'] = risk
9 2186
I don't know what "FORMAT" means, but in the following example add it to df as format1 and format2. - data = {'funds': ['DOMCSP','DOMCSP','DOMFEE','DOMFEE','INTON','INTON','INTOFF','INTOFF'],
-
'risk': [1, 2, 3, 4, 5,6,7,100]}
-
df = pd.DataFrame(data, columns = ['funds', 'risk',])
-
df
-
-
format1=[]
-
format2=[]
-
for i in df["funds"]:
-
if i == 'DOMCSP' or i == 'DOMFEE':
-
format1.append('Domestic')
-
elif i == 'INTON' or i == 'INTOFF':
-
format1.append('International')
-
for j in df["risk"]:
-
if j >= 1 and j <= 3:
-
format2.append('Low')
-
elif j >= 4 and j <= 5:
-
format2.append('Medium')
-
elif j >= 6:
-
format2.append('High')
-
df ["format1"] = format1
-
df ["format2"] = format2
-
print(df)
-
HI There,
Sorry, we use that term format in SAS a lot.
I am talking something like this below - broad_format={"DOMCSP":"Domestic Common Wealth Supported","DOMFEE":"Domestic Fee Paying",
-
"DOMRTS":"Domestic Research Training Scheme",
-
"INTOFF":"International Offshore","INTON":"International"}
-
-
crse_type_format={1:"Higher Doctorate",2:"Doctorate by Research",10:"Bachelor's Pass",
-
11:"Graduate Certificate",12:"Doctorate by Coursework"}
- enrol_v1.replace({"broad_funds":broad_format,"course_type":crse_type_format})
Is it something which can be done just like above.
Will it create a new DataFrame "enrol_v1" from "df" and the conditions? - data = {'funds': ['DOMCSP','DOMCSP','DOMFEE','DOMFEE','INTON','INTON','INTOFF','INTOFF'],
-
'risk': [1, 2, 3, 4, 5,6,7,100]}
-
df = pd.DataFrame(data, columns = ['funds', 'risk',])
-
df
-
enrol_v1 = pd.DataFrame(columns = ['broad_funds','broad_format','course_type','crse_type_format'])
-
-
broad_format=[]
-
crse_type_format=[]
-
for i in df["funds"]:
-
if i == 'DOMCSP' or i == 'DOMFEE':
-
broad_format.append('Domestic')
-
elif i == 'INTON' or i == 'INTOFF':
-
broad_format.append('International')
-
else:
-
broad_format.append('')
-
for j in df["risk"]:
-
if j >= 1 and j <= 3:
-
crse_type_format.append('Low')
-
elif j >= 4 and j <= 5:
-
crse_type_format.append('Medium')
-
elif j >= 6:
-
crse_type_format.append('High')
-
else:
-
crse_type_format.append('')
-
-
enrol_v1 ['broad_funds'] = df['funds']
-
enrol_v1 ['broad_format'] = broad_format
-
enrol_v1 ['course_type'] = df['risk']
-
enrol_v1 ['crse_type_format'] = crse_type_format
-
print(enrol_v1)
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.
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. - Value $Broad_Funds
-
'DOMCSP','DOMRTS','DOMRTP'= " Domestic C'wealth Sup"
-
'DOMFEE'="Domestic Fee-Paying"
-
'INTON','INTRTP' =" International On-shore"
-
'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.
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. - df.replace('DOMCSP', 'Domestic', inplace=True)
-
df.replace('DOMFEE', 'Domestic', inplace=True)
-
df.replace('INTON', 'International', inplace=True)
-
df.replace('INTOFF', 'International', inplace=True)
-
risk=[]
-
for j in df["risk"]:
-
if j >= 1 and j <= 3:
-
risk.append('Low')
-
elif j >= 4 and j <= 5:
-
risk.append('Medium')
-
elif j >= 6:
-
risk.append('High')
-
else:
-
risk.append('')
-
df.drop('risk', axis=1)
-
df ['risk'] = risk
Hi There,
Thanks for the advice.
I will make a note of it.
For reference,
It also shows how direct replacement code uses "slices".
But, this will put a warning message. - l = 0
-
for k in df['risk']:
-
if k >= 1 and k <= 3:
-
df['risk'][l] = 'Low'
-
elif k >= 4 and k <= 5:
-
df['risk'][l] = 'Medium'
-
elif k >= 6:
-
df['risk'][l] = 'High'
-
l = l + 1
Hi There,
Thanks for this.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 %%...
|
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...
|
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?
|
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...
|
by: Newsgroups |
last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function
is?
|
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++)
{
...
|
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?
|
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.
...
|
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>
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| |