我有导入大熊猫的税务数据,我想查看数据的某些部分,以便我可以分析这部分人口。我这样做的方式是创建像这样的新数据框
new_df = old_tax_df[(old_tax_df.var_1 == 1) & (old_tax_df.var_2 == 1)]
有没有办法在不制作数据框的情况下做到这一点,这会使代码更清晰?
答案 0 :(得分:3)
让我们尝试.query
方法,它更具可读性:
public partial class Form1 : Form
{
private FilterInfoCollection captureDevices;
private VideoFileWriter videoWriter;
private VideoCaptureDevice finalVideo;
private Bitmap videoFrame;
public Form1()
{
InitializeComponent();
CameraInit();
}
private void CameraInit()
{
captureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
finalVideo = new VideoCaptureDevice(captureDevices[0].MonikerString);
finalVideo.NewFrame += FinalVideo_NewFrame;
int cameraCapabilitiesCount = finalVideo.VideoCapabilities.Count();
VideoCapabilities cameraCapability = finalVideo.VideoCapabilities[cameraCapabilitiesCount - 1];
videoWriter = new VideoFileWriter();
videoWriter.Open(@"F:/FilmikiTest/testVideo.mp4", cameraCapability.FrameSize.Width, cameraCapability.FrameSize.Height, cameraCapability.AverageFrameRate, VideoCodec.MPEG4, 5000000);
}
private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
videoFrame = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = videoFrame;
videoWriter.WriteVideoFrame(videoFrame);
}
private void button1_Click(object sender, EventArgs e)
{
finalVideo.Start();
}
private void button2_Click(object sender, EventArgs e)
{
finalVideo.Stop();
videoWriter.Close();
}
}
或者正如MaxU在评论中指出的那样。
new_df = old_tax_df.query('var_1 == 1 and var_2 == 1')
也会工作。
答案 1 :(得分:2)
一种方法 -
old_tax_df[(old_tax_df[['var_1','var_2']]==1).all(1)]
示例运行 -
In [68]: old_tax_df
Out[68]:
var_1 var_2 var_3 var_4
0 0 1 0 1
1 1 1 0 2
2 2 1 1 1
3 1 0 1 2
4 1 2 0 2
5 2 0 1 1
6 2 0 0 2
7 0 2 2 0
8 1 1 0 1
9 2 1 1 1
# Original code
In [69]: old_tax_df[(old_tax_df.var_1 == 1) & (old_tax_df.var_2 == 1)]
Out[69]:
var_1 var_2 var_3 var_4
1 1 1 0 2
8 1 1 0 1
# Proposed code
In [70]: old_tax_df[(old_tax_df[['var_1','var_2']]==1).all(1)]
Out[70]:
var_1 var_2 var_3 var_4
1 1 1 0 2
8 1 1 0 1
# Alternative using `.eq` to replace `==1`
In [76]: old_tax_df[old_tax_df[['var_1','var_2']].eq(1).all(1)]
Out[76]:
var_1 var_2 var_3 var_4
1 1 1 0 2
8 1 1 0 1
答案 2 :(得分:0)
.groupby
您可以使用groupby方法,然后访问组。
groups = df.groupby(['var_1', 'var_2', 'var_3', 'var_4'])
groups.get_group((2, 0, 0, 2))
这是根据其中的值将一个数据帧分割为多个数据帧的一种方法。
.loc
df.set_index(['var_1', 'var_2', 'var_3', 'var_4'], inplace=True)
df.loc[2, 0, 0, 2]
将产生相同的结果,除了数据帧现在将在变量字段上编入索引。
有关此主题的更多信息,请参阅Pandas的Indexing and Selecting Data文档页面。