In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the methods of replacing characters in Pandas in batches". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
I. Foreword
A few days ago, a fan named [dcpeng] asked a question about Pandas in Python's strongest king group. Here, I will share it with you and learn together.
I want to ask if I have a column of data coded as 1, 2, 3, 4, how to change 1 batch to "happy", 2 batch to "sad" this character replacement?
II. Settlement process
The idea is quite simple, limited Pandas processing, there are many ways to think of, here to share with you, I hope to help you learn.
Here is the code that generates the source data:
df = pd.DataFrame({'col 1':[1, 2, 2, 3, 3, 4, 4, 4]})df Method 1
The code is as follows:
df['col2'] = df['col1'].map({1:"happy," 2:"sad," 3:"sad," 4:"tears"})df
The operation results are shown in the following figure:
methodology II
This method is written with reference to Cai Ge's article. The code is as follows:
def getValue(s): if s==1: return 'Happy' elif s==2: return 'Grief' elif s==3: return 'sad' elif s==4: return 'Tears'df <$'col 3']= df <$'col 1'].apply(getValue)df
The operation results are shown in the following figure:
method three
The boss gave an idea and used replace to implement it.
The code is as follows:
df <$'col 4'] = df <$'col 1'].replace(1, 'happy').replace(2, ' sad').replace(3, 'sad').replace(4, ' tear') df
The results obtained are as follows:
method four
This method is based on the apply() function and looks like this:
def get_value(s): dict = {1:"happy", 2:"sad", 3:"sad", 4:"tears"} return dict[s]df['col5'] = df['col1'].apply(get_value)df
The operation results are shown in the following figure:
method five
[Shen Fu] Big Boss gave him an idea and code, as shown in the figure below:
This method is based on the map() function and looks like this:
def get_value(s): dict = {1:"happy", 2:"sad", 3:"sad", 4:"tears"} return dict[s]df['col5'] = df['col1'].map(get_value)df
The operation results are shown in the following figure:
method a
Here Moon is still implemented using the replace method, but the code shows a lot.
The code is as follows:
df['col 7']= df['col 1'].replace([1, 2, 3, 4], ['happy',' sad','tear'])df
[Moon Goddess] Reminder: This is a full match, do not add regex=True parameter, or you will regret it!
The operation results are shown in the following figure:
"Pandas batch replacement of characters what are the methods" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.