In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Python how to specify the column front", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Python how to specify the column front" bar!
problem
A small partner asked a simple question, the data are as follows:
I want to adjust the column "key1,2,3" to the far left of the header. I don't care about the order of the other columns.
Don't make me write down all the columns, because some of my tables may have dozens or hundreds of columns
Premise
We need to know some prerequisites.
How to specify the column order in pandas? Very simple:
You can specify multiple list with column names in df [].
Obviously, the questioner just doesn't want to enter all the lists manually.
How to get all the column names of the table in pandas:
Df.columns is fine.
If you want a list, all you need is list (df.columns)
How to concatenate 2 lists in Python:
After understanding these two points, the problem now is not really the order of pandas or some header, but a list construction problem:
Line 4: is the key to solving the problem
Next, based on these prerequisites and ideas, see how many solutions can be made.
Solution 1
This is the most direct way:
Lines 4-7: iterate through all the columns and remove the non-target column names
If you are not familiar with deduction, you can also use for in to traverse
"I don't know anything about deduction, and I'm not familiar with traversing. Is there a way not to traverse?"
Solution 2
"doesn't Python always claim that the syntax is simple? why can't you subtract the two lists and get the remaining column names?"
That makes sense. Why not?
Because the elements in the list can be repeated, if the two lists can be subtracted, he will have to consider the ambiguity between repeating elements and non-repeating elements.
Therefore, the list cannot be subtracted, but there is something in Python that is similar to the list, but the elements in it cannot be repeated-- set.
From this we can get the second solution:
Line 4: use the set function to get the set structure, which can be subtracted.
Why do you need another list on the outside? Because next we need to use the coquettish operation of "list addition"
If you are careful, you may notice that the order of the other columns in the result is different from that of solution 1!
Because set does not care about the order of elements, the order changes after the subtraction of two set
"is there a different traversal, and is there one that can retain the original order of the other columns?"
Solution 3
The previous solution is essentially doing the work of removing weight. Isn't there a deduplication function in pandas? we can also use it.
Passing in a list of repeating elements in the row 4:pd.Series returns a Series. It can be done by using his weight removal method.
"there's a problem. New_cols is a list. Shouldn't it be a Series? why can df [a Series] also change the order of columns?"
In fact, most sequences, such as arrays of set or numpy, can be accepted in df [], but not tuples.
Thank you for your reading, the above is the content of "how to specify the column front of Python". After the study of this article, I believe you have a deeper understanding of how to specify the column front of Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.