我正在尝试创建一个堆叠水平条形图,它是由以下代码创建的,但是我想按频率降序对其进行排序。我尝试在 groupby 行中添加 .sort_values() 但它不会影响图表。
此谷歌驱动器提供的数据:https://drive.google.com/file/d/1PcfYsyD6T1_EHz3FZvdWN2DewzI64_zq/view ?usp=sharing
df = pd.read_csv('data.csv')
Company Location DateTime Details Outcome
0 SpaceX LC-39A, Kennedy Space Center, Florida, USA Fri Aug 07, 2020 05:12 UTC Falcon 9 Block 5 | Starlink V1 L9 & BlackSky Success
1 CASC Site 9401 (SLS-2), Jiuquan Satellite Launch Center, China Thu Aug 06, 2020 04:01 UTC Long March 2D | Gaofen-9 04 & Q-SAT Success
2 SpaceX Pad A, Boca Chica, Texas, USA Tue Aug 04, 2020 23:57 UTC …Run Code Online (Sandbox Code Playgroud) 如何根据多列对 Pandas DataFrame 进行排序,其中一些列按升序排序,而其他列按降序排序?
此外,假设将用于排序的列有太多唯一值,无法为排序键或函数一一列出pd.Categorical(如本线程中建议的)。
这是一个可重现的小示例:
import pandas as pd
my_df = pd.DataFrame({'col1':['a','a','a','a','b','b','b','b','c','c','c','c'],
'col2':[1,1,2,2,1,1,2,2,1,1,2,2],
'col3':[1,2,1,2,1,2,1,2,1,2,1,2]})
Run Code Online (Sandbox Code Playgroud)
假设在上面的示例中,我想my_df按如下方式排序:
col1按降序排列col2按降序排列col3按升序排列我想知道如何在 python3 中通过“col”asc 来排序。尝试排序和 orderDict。
d = {0: {'m1': 32, 'M1': 174, 'm2': 190, 'M2': 967, 'col': 6, 'cols': [2, 6, 11, 14, 15, 16, 18]},
1: {'m1': 50, 'M1': 580, 'm2': 588, 'M2': 960, 'col': 10, 'cols': [2, 6, 7, 10, 11, 14, 15, 16, 17, 18, 19]},
2: {'m1': 35, 'M1': 595, 'm2': 621, 'M2': 972, 'col': 8, 'cols': [2, 6, 10, 11, 14, 15, 16, 18, 19]},
3: {'m1': 41, 'M1': 85, 'm2': 104, 'M2': 962, 'col': …Run Code Online (Sandbox Code Playgroud) 我想问如何对每个优先电子邮件域的字符串数组进行排序?
例如,这是我的优先级列表
1st: gmail.com
2nd: outlook.com
3rd: yahoo.com
4th: hotmail.com
5th: live.com
6th: Other Domain
Run Code Online (Sandbox Code Playgroud)
所以,如果这是我的字符串
const personalEmail = ['email@yahoo.com','email@live.com','email@other.com','email@gmail.com','email@outlook.com']
Run Code Online (Sandbox Code Playgroud)
有哪些方法可以实现这一目标?
假设我有一些代码,例如:
struct GenericStruct {
a: u8,
b: String,
}
fn sort_array(generic_vector: Vec<GenericStruct>) -> Vec<GenericStruct> {
// Some code here to sort a vector.
todo!();
}
fn main() {
let some_words = String::from("Hello Word");
let x = GenericStruct { a: 25, b: some_words };
let some_vector: Vec<GenericStruct> = vec![x];
}
Run Code Online (Sandbox Code Playgroud)
如何根据部分对向量进行排序,例如按 排序a或按 的长度排序b?
我有一个工作代码,但是,我希望学习一个更Pythonic的版本,因为我觉得我的代码非常笨重。
这个想法是计算列表(“名称”)中出现的次数并对其后代进行排序。
aux_list = []
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}
for name in names:
if name is not aux_list:
aux_list.append(name)
count = names.count(name)
key = { name : count }
total.update(key)
# sort desc
sorted_total = sorted(total, key=total.get, reverse=True)
# Desired output: <NAME> <TOTAL> DESC ORDER
# John 3
# Steve 2
# Jessica 1
for r in sorted_total:
print(r, total[r])
Run Code Online (Sandbox Code Playgroud) 我有一个下面的数据框:
col1
Numb10
Numb11
Numb12
Numb7
Numb8
Run Code Online (Sandbox Code Playgroud)
如何按数字顺序排序:
col1
Numb7
Numb8
Numb10
Numb11
Numb12
Run Code Online (Sandbox Code Playgroud)
我尝试过但出现错误TypeError: cannot convert the series to <class 'int'>。
df.sort_values(by = "col1", key = (lambda x: int(x[4:])))
Run Code Online (Sandbox Code Playgroud)
更新一下,缺了一个col1
我想出了一个晦涩的排序算法,鉴于它如此简单,它一定是以前发明和命名的,所以我想知道它叫什么。
它有一个非常罕见的约束:它仅适用于具有从0to n-1(或等效键)键的输入。这是一个非常强的约束,使其在实践中毫无用处,但也许我们可以构建一些让它有用的人为设置。该算法基本上将特定位置的元素与其最终位置交换,直到数组排序为止。伪代码:
def obscure_sort(array):
sorted_until = 1
while true
if key(array[0]) != 0:
# Swap the element at position 0 to its final position.
swap(array, 0, key(array[0]))
else:
# Find the next element that isn't in its final position.
while key(array[sorted_until]) == sorted_until:
sorted_until++
# If we happen to reach the end, we're done.
if sorted_until == array.length:
return
# Swap the newfound first unsorted element to position 0
swap(array, 0, sorted_until)
Run Code Online (Sandbox Code Playgroud)
该算法实际上运行在O(n). 看到这一点并不完全是微不足道的,除非有人真的感兴趣,否则我将省略分析。 …
我有一个由一些共享指针组成的向量。我试图使用排序比较器函数根据某些标准对该向量进行排序。问题是,如果我们尝试从比较器函数内部访问向量的值;在某些地方我得到了空值。仅在共享指针的情况下才会发生这种情况。对于普通指针,我没有发现这样的问题。为什么在这种情况下我们会在向量内得到空值?
我的代码-
class Tmp {
private :
int a,b;
public :
int getA() {
return a;
}
int getB() {
return b;
}
Tmp(int x, int y) : a(x), b(y) {}
};
int main() {
std::shared_ptr<Tmp> t1 = std::make_shared<Tmp>(11,19);
std::shared_ptr<Tmp> t2 = std::make_shared<Tmp>(2,3);
std::shared_ptr<Tmp> t3 = std::make_shared<Tmp>(5,6);
std::shared_ptr<Tmp> t4 = std::make_shared<Tmp>(3,5);
vector<std::shared_ptr<Tmp>> v;
v.push_back(t1);
v.push_back(t2);
v.push_back(t3);
v.push_back(t4);
auto fun = [&v] (std::shared_ptr<Tmp> &l, std::shared_ptr<Tmp> &r) -> bool {
for (auto it : v) {
if (!it) {
cout<<"null value"<<endl;
} …Run Code Online (Sandbox Code Playgroud) 例如,下面的代码按 desc 顺序对 vec 进行排序:
std::vector<int> vec = {1, 2, 5, 4, 3};
sort(vec.rbegin(), vec.rend());
for(const auto v : vec)
std::cout << v << "\n";
output 5 4 3 2 1
Run Code Online (Sandbox Code Playgroud)
关于C++ 参考:
将范围 [first,last) 中的元素按升序排序。对于第一个版本,使用运算符 < 来比较元素 [...]
sorting ×10
python ×5
pandas ×3
algorithm ×2
c++ ×2
arrays ×1
bar-chart ×1
dataframe ×1
dictionary ×1
iterator ×1
javascript ×1
matplotlib ×1
rust ×1
shared-ptr ×1
tuples ×1
vector ×1