What are the two small trick commonly used in data cleaning?
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.