Pandas:根据布尔列表/字典替换数据帧列(Pandas: Replace dataframe columns based on Boolean list/dict)
我有两个pandas数据框,我想合并在一起,但不是我在我能够找到的例子中看到的方式。 我有一组“旧”数据和一组“新”数据,两个数据框的形状相同,列名相同。 我做了一些分析并确定我需要创建第三个数据集,从“旧”数据中获取一些列,从“新”数据中获取一些列。 举个例子,假设我有这两个数据集:
df_old = pd.DataFrame(np.zeros([5,5]),columns=list('ABCDE')) df_new = pd.DataFrame(np.ones([5,5]),columns=list('ABCDE'))
简单地说:
A B C D E 0 0.0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 0.0 4 0.0 0.0 0.0 0.0 0.0
和
A B C D E 0 1.0 1.0 1.0 1.0 1.0 1 1.0 1.0 1.0 1.0 1.0 2 1.0 1.0 1.0 1.0 1.0 3 1.0 1.0 1.0 1.0 1.0 4 1.0 1.0 1.0 1.0 1.0
我做了一些分析,发现我想要替换
B
列和D
列。 我可以在这样的循环中做到这一点:replace = dict(A=False,B=True,C=False,D=True,E=False) df = pd.DataFrame({}) for k,v in sorted(replace.items()): df[k] = df_new[k] if v else df_old[k]
这给了我想要的数据:
A B C D E 0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 1.0 0.0 2 0.0 1.0 0.0 1.0 0.0 3 0.0 1.0 0.0 1.0 0.0 4 0.0 1.0 0.0 1.0 0.0
但是,老实说这看起来有点笨重,而且我想有一种更好的方法来使用熊猫来做到这一点。 另外,我想保留我的列的顺序,这可能不是像这个示例数据集那样按字母顺序排列,所以排序字典可能不是可行的方法,尽管如果我可以从数据集中提取列名,需要。
有没有更好的方法来使用一些Pandas合并功能?
I have two pandas data-frames that I would like to merge together, but not in the way that I've seen in the examples I've been able to find. I have a set of "old" data and a set of "new" data that for two data frames that are equal in shape with the same column names. I do some analysis and determine that I need to create third dataset, taking some of the columns from the "old" data and some from the "new" data. As an example, lets say I have these two datasets:
df_old = pd.DataFrame(np.zeros([5,5]),columns=list('ABCDE')) df_new = pd.DataFrame(np.ones([5,5]),columns=list('ABCDE'))
which are simply:
A B C D E 0 0.0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 0.0 4 0.0 0.0 0.0 0.0 0.0
and
A B C D E 0 1.0 1.0 1.0 1.0 1.0 1 1.0 1.0 1.0 1.0 1.0 2 1.0 1.0 1.0 1.0 1.0 3 1.0 1.0 1.0 1.0 1.0 4 1.0 1.0 1.0 1.0 1.0
I do some analysis and find that I want to replace columns
B
andD
. I can do that in a loop like this:replace = dict(A=False,B=True,C=False,D=True,E=False) df = pd.DataFrame({}) for k,v in sorted(replace.items()): df[k] = df_new[k] if v else df_old[k]
This gives me the data that I want:
A B C D E 0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 1.0 0.0 2 0.0 1.0 0.0 1.0 0.0 3 0.0 1.0 0.0 1.0 0.0 4 0.0 1.0 0.0 1.0 0.0
but, this honestly seems a bit clunky, and I'd imagine that there is a better way to use pandas to do this. Plus, I'd like to preserve the order of my columns which may not be in alphabetical order like this example dataset, so sorting the dictionary may not be the way to go, although I could probably pull the column names from the data set if need be.
Is there a better way to do this using some of Pandas merge functionality?
原文:
最满意答案
只需在您的应用程序中包含所有必需的文件,它将在x86和x64上运行 - 请参阅我的博客文章: http : //erikej.blogspot.com/2011/02/using-sql-server-compact-40- with.html
您还可以使用下面的代码来检测您的应用程序是否可以使用运行时,但如果yu在上面实现,则不需要:
public bool IsV40Installed() { try { System.Reflection.Assembly.Load("System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"); } catch (System.IO.FileNotFoundException) { return false; } try { var factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0"); } catch (System.Configuration.ConfigurationException) { return false; } catch (System.ArgumentException) { return false; } return true; }
Just include all the required files with your app, and it will run on both x86 and x64 - see my blog post here: http://erikej.blogspot.com/2011/02/using-sql-server-compact-40-with.html
You can also use code below to detect if the runtime is available to your app, but not required if yu implement above:
public bool IsV40Installed() { try { System.Reflection.Assembly.Load("System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"); } catch (System.IO.FileNotFoundException) { return false; } try { var factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0"); } catch (System.Configuration.ConfigurationException) { return false; } catch (System.ArgumentException) { return false; } return true; }
相关问答
更多-
调用Fill后,SQL Server CE 4.0不会立即更新(SQL Server CE 4.0 not updating immediately after call to Fill)[2024-01-09]
SQL CE只会间隔刷新到磁盘,并且可以通过连接字符串进行更改: “指定在将所有已提交的事务刷新到磁盘之前的间隔时间(以秒为单位)。如果未指定,则默认值为10.” 不要在连接字符串中包含该设置,您将面临丢失数据的风险: http : //erikej.blogspot.dk/2016/02/sql-server-compact-40-sp1-hotfix.html 您可以通过使用CommitMode为Immediate的SqlCeTransaction包装来强制它进行刷新。 http://erikej.bl ... -
在您的参考中使用特定的version = true Use specific version = true in your reference
-
您想要显示所有产品,但只能将状态为1的产品加起来吗? 如果是这样,我认为您需要在您的加入中使用子查询。 尝试这个: Select a.code, a.Naam, a.Voorraad, isnull(sum(orders.Hoeveelheid)-sum(orders.Voldaan)-sum(orders.Geannuleerd),0) as Besteld from Artikels a left join ( select bd.ArtikelID, bd.BestelID, bd. ...
-
取决于您的计划的性质。 了解SQL CE最适合部署的客户端应用程序,因为SQL CE可以私下部署 。 首次加载程序时,应检查数据库文件是否已存在。 如果数据库不存在,您应该创建数据库并创建相应的表。 客户端计算机必须安装网络框架! 此外 ,您必须要求最终用户可以使用公共安装 ,或者在应用程序中私下部署相应的类库。 Depends on the nature of your program. Understand that SQL CE is most suitable for deployed client ...
-
EF Core支持SQLite,是的。 EF Core支持SQL Compact,但仅限Windows桌面。 使用Xamarin Forms支持EF Core仍在进行中,请参阅: https : //docs.microsoft.com/en-us/ef/efcore-and-ef6/features EF Core supports SQLite, yes. And SQL Compact is supported with EF Core, but only on Windows desktop. Su ...
-
SqlCeConnection和相关对象不能跨线程共享,这可能就是您所面临的问题。 为每个线程创建一个新对象。 After much experimentation I found that wrapping the database calls inside a transaction solved the problem. So instead of the original code like this... using (AMSDBContext context = CreateDatabaseCo ...
-
必须授予对相应app_pool用户的读/写访问权限。 在这种情况下, IIS APPPOOL\DefaultAppPool 请注意,必须如图所示指定用户组。 One must grant read/write access to the appropriate app_pool user. In this case, IIS APPPOOL\DefaultAppPool Note that the user group must be specified as shown.
-
我实际上刚刚在博客上写了一段时间。 http://enterpriseyness.com/2011/11/deploying-an-asp-net-web-application-with-sql-ce-4-entity-framework-without-installation/ 如果您轻松点击Web项目,您将看到“添加可部署的依赖项”菜单项。 另外,不要忘记将SQL CE提供程序工厂添加到Web.config中。 Just to update how we solved this problem, w ...
-
Sql metal不能与sqlce 4.0一起使用(我试过),你必须使用linq2entity或手工编写你的数据对象和POCOS。 或者等到下一个版本的视觉工作室。 我希望微软仍然会支持sqlq4.0的linq2sql,因为它很容易使用。 Sql metal doesnt work with sqlce 4.0 (i tried), you must use linq2entity or code your data object and POCOS by hand. Or wait till the ne ...
-
只需在您的应用程序中包含所有必需的文件,它将在x86和x64上运行 - 请参阅我的博客文章: http : //erikej.blogspot.com/2011/02/using-sql-server-compact-40- with.html 您还可以使用下面的代码来检测您的应用程序是否可以使用运行时,但如果yu在上面实现,则不需要: public bool IsV40Installed() { try { System.Re ...