我有一个数据框,如:
foo bar qux
0 a 1 3.14
1 b 3 2.72
2 c 2 1.62
3 d 9 1.41
4 e 3 0.58
Run Code Online (Sandbox Code Playgroud)
我想在数据帧的末尾添加一个"总"行:
foo bar qux
0 a 1 3.14
1 b 3 2.72
2 c 2 1.62
3 d 9 1.41
4 e 3 0.58
5 tot 15 9.47
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用该sum命令,但我最终得到了一个系列,虽然我可以转换回Dataframe,但不维护数据类型:
tot_row = pd.DataFrame(df.sum()).T
tot_row['foo'] = 'tot'
tot_row.dtypes:
foo object
bar object
qux object
Run Code Online (Sandbox Code Playgroud)
我想维护原始数据框中的数据类型,因为我需要将其他操作应用于总行,例如:
baz = 2*tot_row['qux'] + 3*tot_row['bar']
Run Code Online (Sandbox Code Playgroud) 在经历了许多痛苦和痛苦之后,我已经找到了一些非常奇怪的行为,std::distance当给出一系列boost::filter_iterators超过a 时,它永远不会返回std::deque.看来问题是GCC(6.1+)的-O3优化问题.这是一个演示违规行为的示例:
#include <string>
#include <deque>
#include <iterator>
#include <iostream>
#include <boost/iterator/filter_iterator.hpp>
struct Foo
{
std::string bar, s = "";
char a = '\0';
};
int main()
{
const std::deque<Foo> foos(14, {""});
const std::string test {};
const auto p = [test] (const auto& foo) { return foo.bar == test; };
using boost::make_filter_iterator;
const auto begin = make_filter_iterator(p, std::cbegin(foos), std::cend(foos));
const auto end = make_filter_iterator(p, std::cend(foos), std::cend(foos));
std::cout << std::distance(begin, end) << std::endl; …Run Code Online (Sandbox Code Playgroud) 为什么没有任何std::algorithm方法constexpr?如果我正确理解新的C++ 14规则,那么很多方法都可以constexpr.例如,为什么不能std::find有constexpr?
static constexpr std::array<char, 4> DnaBases {'A', 'C', 'G', 'T'};
constexpr bool is_dna(char b)
{
return std::find(std::cbegin(DnaBases), std::cend(DnaBases), b) != std::cend(DnaBases); // why not?
}
Run Code Online (Sandbox Code Playgroud)
还有哪些std::algorithm可能constexpr?
有没有办法在matplot lib饼图中更改百分比标签的默认位置?
这是一个示例饼图:

我用它创建的:
plt.pie(sizes, labels=labels, colors=colors, explode=explode, autopct='%1.0f%%')
Run Code Online (Sandbox Code Playgroud)
现在我不喜欢一些百分比标签如何侵入其他部分teritory(实际上这个例子中唯一的perpitrator是9m部分).理想情况下,我希望这些标签位于饼图之外,某些箭头指向该部分,或者可选地在该部分之外.
有没有办法安置std::pair?
std::unordered_map<int, std::pair<std::string, std::string>> my_map;
my_map.emplace(1, "foo", "bar"); // Error
Run Code Online (Sandbox Code Playgroud)
当然可以插入:
my_map[2] = std::make_pair("bar", "foo");
Run Code Online (Sandbox Code Playgroud)
但是这不需要不必要的复制/移动吗?
我想从我的seaborn lineplot图例中删除标题.我尝试使用这个答案无济于事:
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
fmri = sns.load_dataset("fmri")
fig, ax = plt.subplots()
g = sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri, ax=ax)
ax.legend().set_title('')
Run Code Online (Sandbox Code Playgroud)
如果我尝试将标题设置为,我会得到相同的结果None.有趣的是,将标题设置为其他内容似乎优先于现有标题:
ax.legend().set_title('Something else')
Run Code Online (Sandbox Code Playgroud)
看起来seaborn几乎将标题视为隐藏的传奇条目.我该如何解决这个问题?
有没有办法检查序列容器在内存中是否连续?就像是:
#include <iostream>
#include <vector>
#include <deque>
#include <array>
int main()
{
std::cout << std::boolalpha;
std::cout << is_contiguous<std::vector<int>>::value << '\n' // true
std::cout << is_contiguous<std::deque<int>>::value << '\n'; // false
std::cout << is_contiguous<std::array<int, 3>>::value << '\n'; // true
}
Run Code Online (Sandbox Code Playgroud)
澄清
这个问题是指类型特征,而不是特定类型实例的属性.
假设我们有class一个std::mutex:
class Foo
{
std::mutex mutex_;
std::string str_;
// other members etc
public:
friend void swap(Foo& lhs, Foo& rhs) noexcept;
}
Run Code Online (Sandbox Code Playgroud)
在swap这里实施该方法的适当方法是什么?是否需要/安全地分别锁定每个互斥锁然后交换所有内容?例如
void swap(Foo& lhs, Foo& rhs) noexcept
{
using std::swap;
std::lock_guard<std::mutex> lock_lhs {lhs.mutex_}, lock_rhs {rhs.mutex_};
swap(ls.str_, rhs.str_);
// swap everything else
}
Run Code Online (Sandbox Code Playgroud)
我已经看到在C++ 17中,std::lock_guard将有一个构造函数采用多个互斥锁来避免死锁,但我不确定这是否是一个问题?
我正在使用一个应用程序,用于从文本文件中std::stringstream读取空格分隔的doubles 矩阵.该应用程序使用的代码有点像:
std::ifstream file {"data.dat"};
const auto header = read_header(file);
const auto num_columns = header.size();
std::string line;
while (std::getline(file, line)) {
std::istringstream ss {line};
double val;
std::size_t tokens {0};
while (ss >> val) {
// do stuff
++tokens;
}
if (tokens < num_columns) throw std::runtime_error {"Bad data matrix..."};
}
Run Code Online (Sandbox Code Playgroud)
很标准的东西.我努力编写一些代码来制作数据矩阵(data.dat),对每个数据行使用以下方法:
void write_line(const std::vector<double>& data, std::ostream& out)
{
std::copy(std::cbegin(data), std::prev(std::cend(data)),
std::ostream_iterator<T> {out, " "});
out << data.back() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
即使用std::ostream.但是,我发现应用程序无法使用此方法读取我的数据文件(抛出上面的异常),特别是它无法读取7.0552574226130007e-321. …
我有时会发现需要编写可以应用于对象容器的通用例程,或者这些容器的映射(即处理映射中的每个容器).一种方法是为地图类型编写单独的例程,但我认为有一个例程适用于两种类型的输入可能更自然,更简洁:
template <typename T>
auto foo(const T& items)
{
return foo(items, /* tag dispatch to map or non-map */);
}
Run Code Online (Sandbox Code Playgroud)
什么是安全,干净的方式来执行此标签发送?
c++ ×7
c++11 ×4
c++14 ×3
python ×3
gcc ×2
matplotlib ×2
type-traits ×2
algorithm ×1
clang ×1
constexpr ×1
containers ×1
deque ×1
dictionary ×1
iostream ×1
line-plot ×1
mutex ×1
pandas ×1
seaborn ×1
stl ×1
swap ×1