我有2个pandas数据帧。第二个包含在第一个中。如何将第一个中的值替换为第二个中的值?
考虑这个例子:
df1 = pd.DataFrame(0, index=[1,2,3], columns=['a','b','c'])
df2 = pd.DataFrame(1, index=[1, 2], columns=['a', 'c'])
ris= [[1, 0, 1],
[1, 0, 1],
[0, 0, 0]]
和ris
具有相同的d1
可能的解决方案是:
for i in df2.index:
for j in df2.columns:
df1.loc[i, j] = df2.loc[i, j]
但这很丑陋
答案 0 :(得分:1)
我认为您可以copy
使用combine_first
:
#include <pcl/io/obj_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/PCLPointCloud2.h>
void
printHelp (int,
char **argv)
{
PCL_ERROR ("Please enter input.obj \n", argv[0]);
}
boost::shared_ptr<pcl::visualization::PCLVisualizer> simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
// --------------------------------------------
// -----Open 3D viewer and add point cloud-----
// --------------------------------------------
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
int
main (int argc,
char** argv)
{
PCL_INFO ("Display Obj file. For more information, use: %s -h\n", argv[0]);
if (argc < 2)
{
printHelp (argc, argv);
return (-1);
}
std::vector<int> obj_file_indices = pcl::console::parse_file_extension_argument (argc, argv, ".obj");
if (obj_file_indices.size () != 1)
{
PCL_ERROR ("Need one input OBJ file.\n");
return (-1);
}
pcl::PointCloud<pcl::PointXYZ>::Ptr Cloud_loaded (new pcl::PointCloud<pcl::PointXYZ> ());
pcl::io::loadOBJFile (argv[obj_file_indices[0]], *Cloud_loaded);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
viewer = simpleVis(Cloud_loaded);
while (!viewer->wasStopped ()){
viewer->spinOnce (100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
}
下一个解决方案是在df3 = df1.copy()
df1[df2.columns] = df2[df2.columns]
print df1.combine_first(df3)
a b c
1 1.0 0 1.0
2 1.0 0 1.0
3 0.0 0 0.0
中使用DataFrame
和df4
创建空的新index
columns
并填充加倍combine_first
:
df1
答案 1 :(得分:0)
尝试
{{1}}
如果列名称未知:
{{1}}