English 中文(简体)
如果一栏需要具体测绘,我如何在数据框架内划出多个栏目?
原标题:How do I sort multiple columns in a dataframe, if one column requires a specific mapping?

我有一个数据框架,我想根据三栏进行分类,其中一栏特别有我想使用的具体地图。 例如,就这一数据框架而言:

“enterography

我想按以下几栏分类:<代码>Code,Item,和Type,但我特别要分类。 具体图象(<>t/code>,即指Admin、工作人员、用户、客户(因为我知道Item栏>的“Code/code>栏将始终至少有4项相同价值(例如所举的例子中的1和2)。 因此,我希望我的数据框架照此办理:

enter image description here

In Python, I know how to sort by multiple columns, and I also know how to sort by a particular mapping, but I don t know how to do both at the same time, so if anyone could provide some help I would greatly appreciate it, thank you!

问题回答

<><><>>>>

http://www.un.org/Depts/DGACM/index_french.htm func

# your order of Type column
order = [ Admin ,  Staff ,  User ,  Customer ]

# make mapper of your Type column order 
m = {key: n for n, key in enumerate(order)}

# sort by Code, Item and mapped Type column
页: 1 = df.sort_values(
    [ Code ,  Item ,  Type ], 
    key=lambda x: x.map(m) if x.name ==  Type  else x
)

页: 1

  Code  Item      Type
3  AAA     1     Admin
2  AAA     1     Staff
1  AAA     1      User
0  AAA     1  Customer
6  AAA     2     Admin
4  AAA     2     Staff
5  AAA     2      User
7  AAA     2  Customer
8  BBB     1      User

www.un.org/spanish/ecosoc 例

在法典或文本中树立榜样,而不是形象。

import pandas as pd

data = { Code : [ AAA ,  AAA ,  AAA ,  AAA ,  AAA ,  AAA ,  AAA ,  AAA ,  BBB ],
         Item : [1, 1, 1, 1, 2, 2, 2, 2, 1],
         Type : [ Customer ,  User ,  Staff ,  Admin ,  Staff ,  User ,  Admin ,  Customer ,  User ]}

df = pd.DataFrame(data)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...