Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add a new categorical column to an existing table python

d = df.groupby('Item_Identifier')['Sales'].mean().to_dict()
print (d)
{'Beef': 3030.0, 'Milk': 1233.3333333333333, 'Tea': 150.0}

print (df['Item_Identifier'].map(d))
0    1233.333333
1    1233.333333
2    1233.333333
3    3030.000000
4    3030.000000
5     150.000000
6     150.000000
7     150.000000
Name: Item_Identifier, dtype: float64

bins = [df['Sales'].min(),500, 1500, df['Sales].max()]
labels=['low','medium','high']
df['Price Category'] = pd.cut(df['Item_Identifier'].map(d), bins=bins, labels=labels)
print (df)
  Item_Identifier  Sales Price Category
0            Milk    500         medium
1            Milk   1200         medium
2            Milk   2000         medium
3            Beef     60           high
4            Beef   6000           high
5             Tea    150            low
6             Tea    100            low
7             Tea    200            low
Comment

PREVIOUS NEXT
Code Example
Python :: mk virtual env 
Python :: pandas typr of each cell in series 
Python :: randomforestclassifier 
Python :: how to get entitys of word using pytho nlp 
Python :: zeromq pub sub example python 
Python :: ios iterate through dictionary 
Python :: python creare decoratori 
Python :: Merge the values for each key using an associative and commutative reduce function. 
Python :: top automotive blogs 
Python :: django voice lib 
Python :: get id widget tkinter 
Python :: how to deploy a file size greater than 100mb on pythonanywhere 
Python :: how to create a custom function in python 
Python :: install iris 
Python :: discord.py find channel by id 
Python :: how to see a full row in pandas 
Python :: height and width of colorbar 
Python :: pysft connection drop issue 
Python :: how to create list python 
Python :: python list comprehension with filter example 2 
Python :: *args **kwargs together in python 
Python :: detail view use slug or anything else pk 
Python :: pandas get cvvlaue from antoiher column fom one coluikmnn value 
Python :: 3.81/(1000*1000*100) 
Python :: numpy count occurrences in interval array 
Python :: How to estimate memory of dataset using python command 
Python :: how to check if a word is a palindrome in python 
Python :: Python Multiline docstring example 
Python :: WAP THAT ASKS A USER FOR A NUMBER OF YEARS AND THEN PRINTS OUT THE NUMBER OF DAYS, HOURS ,MINUTES AND SECONDS IN THAT NO. OF YEARS. 
Python :: load data(review path) python 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =