如何按特定的列名对熊猫的数据框进行排序? 我的数据框列如下所示:
+-------+-------+-----+------+------+----------+
|movieId| title |drama|horror|action| comedy |
+-------+-------+-----+------+------+----------+
| |
+-------+-------+-----+------+------+----------+
我只想按列= ['drama','horror','sci-fi','comedy']对数据框进行排序。所以我得到以下数据框:
+-------+-------+------+------+------+----------+
|movieId| title |action|comedy|drama | horror |
+-------+-------+------+------+------+----------+
| |
+-------+-------+------+------+------+----------+
我尝试了df = df.sort_index(axis=1)
,但它对所有列进行了排序:
+-------+-------+------+------+-------+----------+
|action | comedy|drama |horror|movieId| title |
+-------+-------+------+------+-------+----------+
| |
+-------+-------+------+------+-------+----------+
答案 0 :(得分:1)
在第二列之后对所有列进行排序,并添加前两列:
c = df.columns[:2].tolist() + sorted(df.columns[2:].tolist())
print (c)
['movieId', 'title', 'action', 'comedy', 'drama', 'horror']
此列表对列的最后更改顺序:
df1 = df[c]
另一个想法是使用DataFrame.sort_index
,但仅用于DataFrame.iloc
未选择前2个的所有列:
df.iloc[:, 2:] = df.iloc[:, 2:].sort_index(axis=1)
答案 1 :(得分:0)
您可以像这样显式重新排列列
token
如果您有很多列要按字母顺序排序
private void ApplyBackground(string sourceFilename, string backgroundPdf, int pageNumber) {
PdfDocument srcDocument = new PdfDocument(new PdfReader(sourceFilename));
PdfDocument bgDocument = new PdfDocument(new PdfReader(backgroundPdf));
PdfDocument destDocument = new PdfDocument(new PdfWriter(@"C:\Desktop\result.pdf").SetSmartMode(true));
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++) {
srcDocument.CopyPagesTo(i, i, destDocument);
bgDocument.CopyPagesTo(1, 1, destDocument);
}
srcDocument.Close();
bgDocument.Close();
destDocument.Close();
}