我有一个索引/值列表,如下所示:
19, 11, 43
19, 12, 23
19, 13, 2
19, 14, 90
20, 11, 12
20, 12, 0
20, 13, 98
20, 14, 2
21, 11, 43
21, 12, 8
21, 13, 55
21, 14, 34
我需要一个方法,最好使用Python,使用column1和column2将其转换为i x j
矩阵作为i & j
索引,而矩阵内的值对应于column3,因此结果如下所示:
11 12 13 14
19 43 23 2 90
20 12 0 98 2
21 43 8 55 34
有人可以就如何实现这个目的给我建议吗?
答案 0 :(得分:0)
希望您可以使用熊猫,如果可以的话,这是非常直接的:
<强>代码:强>
import pandas as pd
df = pd.read_csv(data_file, header=None, names=['col1', 'col2', 'col3'])
print(df)
pivot = df.pivot(index='col1', columns='col2', values='col3')
print(pivot)
测试数据:
from io import StringIO
data_file = StringIO(u'\n'.join([x.strip() for x in """
19, 11, 43
19, 12, 23
19, 13, 2
19, 14, 90
20, 11, 12
20, 12, 0
20, 13, 98
20, 14, 2
21, 11, 43
21, 12, 8
21, 13, 55
21, 14, 34
""".split('\n')[1:-1]]))
<强>结果:强>
col1 col2 col3
0 19 11 43
1 19 12 23
2 19 13 2
3 19 14 90
4 20 11 12
5 20 12 0
6 20 13 98
7 20 14 2
8 21 11 43
9 21 12 8
10 21 13 55
11 21 14 34
col2 11 12 13 14
col1
19 43 23 2 90
20 12 0 98 2
21 43 8 55 34
答案 1 :(得分:0)
这个问题需要澄清哪些数据结构应该托管结果。 您可以使用嵌套的核心数据结构,但通常使用pandas或numpy。
我的numpy解决方案
public class TranslucentTable extends JTable {
public TranslucentTable() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getBackground());
g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
getUI().paint(g2d, this);
g2d.dispose();
}
@Override
protected JTableHeader createDefaultTableHeader() {
JTableHeader header = new TranslucentTableHeader();
header.setColumnModel(getColumnModel());
return header;
}
}
返回一个numpy矩阵对象
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
String[] colNames = {"Aircraft",
"Track ID",
"Runway",
"Operation Type",
"Number Daily of Operations"};
Object[][] data = {{"Boeing 717",(long) 2459,"01L","Arrival",0.13},{"Boeing 727",(long) 2439,"01R","Arrival",0.12}};
JTable table = new TranslucentTable();
DefaultTableModel model = new DefaultTableModel(data, colNames);
table.setModel(model);
table.setFont(new Font("Serif", Font.BOLD, 20));
table.getTableHeader().setFont(new Font("Serif", Font.BOLD, 20));
table.getTableHeader().setBackground(new Color(0, 0, 0, 100));
table.getTableHeader().setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLACK));
table.getTableHeader().setForeground(Color.WHITE);
table.setRowHeight(table.getRowHeight() + table.getFont().getSize());
table.setBackground(new Color(214, 217, 223));
table.setAutoCreateRowSorter(true);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 0; i < colNames.length; i++) {
table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.RED);
TranslucentScrollPane scrollPane = new TranslucentScrollPane();
scrollPane.setBackground(new Color(0, 0, 0, 200));
scrollPane.setViewportView(table);
frame.add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
(我已经以你关心矩阵的方式阅读了这个问题,所以我省略了索引。)