Creating a new column with numbers in Pandas to group with a column with existing numbers

2024/11/18 2:51:09

Good day,

I have a column from a data frame here:

 A231011 22

My objective is to create a new column and associate the numbers like this:

A     file_number
23        8
10        6
11        6
22        8

As seen above both numbers 22, 23 are associated with the number 8 and numbers 10 and 11 are associated with number 6. How can I create such a column? Thanks in advance

Answer

I think need if need create new values by first value of number with map by dictionary:

print (df['A'].apply(type))
0    <class 'int'>
1    <class 'int'>
2    <class 'int'>
3    <class 'int'>
Name: A, dtype: objectdf['new'] = (df['A'] // 10).map({1:6, 2:8})
print (df)A  new
0  23    8
1  10    6
2  11    6
3  22    8

Detail:

print ((df['A'] // 10))
0    2
1    1
2    1
3    2
Name: A, dtype: int64

Another solution works with strings:

df['new'] = df['A'].astype(str).str[0].map({'1':6, '2':8})

print (df['A'].apply(type))
0    <class 'str'>
1    <class 'str'>
2    <class 'str'>
3    <class 'str'>
Name: A, dtype: objectdf['new'] = df['A'].str[0].map({'1':6, '2':8})

If need convert positive number to first numeric is possible use this solution converted to numpy/pandas:

df['new'] = df['A'] // 10 ** np.log10(df['A'].values).astype(int)print (df)A  new
0       2    2
1   10000    1
2     110    1
3  220000    2
https://en.xdnf.cn/q/120128.html

Related Q&A

How to get PDF file from the binary data of SoftLayers quote?

I got the binary data by "getPdf" method of SoftLayers API.Ref. BillingSoftLayer_Billing_Order_Quote::getPdf | SoftLayer Development Network - http://sldn.softlayer.com/reference/services/Sof…

How to list of elements and use those elements as a header of pandas dataframe?

I have a list with some elements. For example: list= [name, phone_number,age,gender] I want to use these elements as a header or column name in a pandas dataframe. I would really appreciate your ideas.…

Averaging Filter using python

I am new in python and trying apply averaging filter on image as the way i understand averaging concept summing up the neighboring elements including itself and divide it by number of elementstechnique…

Click on Show more deals in webpage with Selenium

Id like to click on every Show 10 more deals on the following page: "https://www.uswitch.com/broadband/compare/deals_and_offers/" but it does not seem to work. Im stuck having the following e…

In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]

This question already has answers here:Changing the tick frequency on the x or y axis(15 answers)Closed 6 years ago.When generating a Matplotlib line or scatter plot, what axis attribute specifies the …

How can I make a simple calculator in python 3?

Im making this calculator using python 3, and this is what I have so far:print("Welcome to Calculator!")class Calculator:def addition(self,x,y):added = x + yreturn addeddef subtraction(self,x…

python 3: lists dont change their values

So I am trying to change a bunch of list items by a random percentage using a for loop.import random as rdm list = [1000, 100, 50, 25] def change():for item in list:item = item + item*rdm.uniform(-10, …

Using zip_longest on unequal lists but repeat the last entry instead of returning None

There is an existing thread about this Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped But its not quite Im after. Instead of returning None, …

python scrapy not crawling all urls in scraped list

I am trying to scrape information from the pages listed on this page. https://pardo.ch/pardo/program/archive/2017/catalog-films.htmlthe xpath selector:film_page_urls_startpage = sel.xpath(//article[@cl…

Python - Do (something) when event is near [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…