简体   繁体   English

如何比较两个相同大小的数据框并创建一个新的数据框,而在列中没有具有相同值的行

[英]How to compare two dataframes of the same size and create a new one without the rows that have the same value in a column

I am creating a data acquisition device that retrieves sensor data (from an API) every 5 minutes and saves it in CSV files (exported every 24h to a database) and I would like to decrease the size of these files by only saving the data when the value changes. 我正在创建一个数据采集设备,该设备每5分钟检索一次传感器数据(从API)并将其保存在CSV文件中(每24小时导出到数据库中),我想通过仅在以下情况下保存数据来减小这些文件的大小:价值改变了。

My idea is to save all the data in a "memory" CSV file (which will be deleted at the end of the day) and to compare the last X lines (df1 -> T1) with the new dataframe (df2 -> T2) and to create the dataframe (df3 -> T2) without the lines where the values remain the same. 我的想法是将所有数据保存在“内存” CSV文件中(该文件将在一天结束时删除),并将最后X行(df1-> T1)与新数据帧(df2-> T2)比较并创建数据框(df3-> T2),而各行的值保持不变。 This df3 will be written in another CSV which will be exported to the database at the end of the day. 此df3将以另一个CSV格式编写,并在一天结束时导出到数据库中。

Is this the right way to proceed ? 这是正确的方法吗?

How to compare two dataframes of the same size and create a 3rd dataframe without the rows where the value does not change ? 如何比较两个相同大小的数据帧,并创建第三个数据帧,而没有值不变的行?

df1 
   Time   Name  Value
0   t1  Name1      3
1   t1  Name2      1
2   t1  Name3      5
3   t1  Name4      9 

df2 
   Time   Name  Value
0   t2  Name1      3
1   t2  Name2      7
2   t2  Name3      5
3   t2  Name4      2 

df3 
   Time   Name  Value
0   t2  Name2      7
1   t2  Name4      2

Use DataFrame.merge with indicator and filter only right_only rows: DataFrame.merge与指标一起使用,并仅过滤right_only行:

df = (df1.merge(df2, on=['Name','Value'], indicator=True, how='outer', suffixes=('_',''))
        .query('_merge == "right_only"')[df2.columns])

print (df)
  Time   Name  Value
4   t2  Name2      7
5   t2  Name4      2

采用:

df3 = df2[df2['value'] != df1['value']]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两个数据框并为同一行中两列相同的条目创建一个新的 - How to compare two dataframes and create a new one for those entries which are the same across two columns in the same row 如何连接两个具有相同列的 Pandas 数据帧,但前提是两个数据帧中的一列的值相同? - How to concat two Pandas dataframes that have the same columns but only if the value of one column in both dataframes is the same? 比较两个不同大小的数据帧并在 Pandas 中创建一个新列 - Compare two dataframes with different size and create a new column in Pandas 使用一列比较具有相同索引的两个数据帧 - Compare two dataframes with same index using one column Pandas,如何将一行中的值与同一列中的所有其他行进行比较,并将其作为新列中的新行值添加? - Pandas, how to compare the value from one row with all other rows in the same column and add it as a new row value in a new column? 如果相同的字符串位于已排序数据框的第一列中,则获取与唯一值关联的行并创建新的数据框 - If the same string is in the first column of a sorted dataframe take the rows associated with the unique value and create new dataframes 如何组合 pandas dataframe 中在一列中具有相同值的行 - How to combine rows in a pandas dataframe that have the same value in one column 如何比较两列不同的数据框并创建一个新的 - How to compare two columns of diffrent dataframes and create a new one 如何获得在一列中具有多个相同值的下两行行的总值计数? - How can I get the total value count of the next two rows of rows that have more than one same value in a column? 如何合并两个具有不同列名但行数相同的数据框? - How to merge two dataframes with different column names but same number of rows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM