In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the two small trick commonly used in data cleaning, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Pandas uses str.split and str.cat skillfully
Because of the above two methods, operate directly according to the column, so leave out a layer of for loop, let's go straight to the example.
Df = pd.DataFrame ({'names': ["Geordi La Forge", "Deanna Troi", "Jack"],' IDs': [1mem2mem3]})
Df
Column partition
For the names column, split into two columns according to the first space:
Df ["first_name"] = df ["names"] .str.split (n = 1) .str [0]
Df ["last_name"] = df ["names"] .str.split (n = 1) .str [1]
Df
The results are as follows:
Column merge method 1
Split the columns, then merge them back, using the cat method:
Df ["names_copy"] = df ["first_name"] .str.cat (df ["last_name"], sep = "")
Df
Merge two columns to get a new column names_copy done!
Column merge method 2
Is there any other way to merge? directly use the + connection string:
Df ["names_copy2"] = df ["first_name"] + "" + df ["last_name"]
Df
The effect is the same:
Pandas multi-condition selection of writing methods with good readability
There are hundreds of features, and when filtering DataFrame based on multiple features, if you do so, the readability is not very friendly:
Df [(df ["continent"] = = "Europe") & (df ["beer_servings"] > 150) & (df ["wine_servings"] > 50) & (df ["spirit_servings"]
< 60)] 连续多个筛选条件写到一行里。 更好可读性的写法cr1 = df["continent"] == "Europe" cr2 = df["beer_servings"] >one hundred and fifty
Cr3 = df ["wine_servings"] > 50
Cr4 = df ["spirit_servings"] < 60
Df [cr1 & cr2 & cr3 & cr4]
After reading the above, do you have any further understanding of the two small trick commonly used in data cleaning? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.