gpt4 book ai didi

python - 比较数据帧后,输出 CSV 未返回所有行

转载 作者:行者123 更新时间:2023-12-04 00:55:23 26 4
gpt4 key购买 nike

我找到了一个可以比较两个数据帧的函数。在使用时,它似乎正确地比较了它们并且它使用的是外部连接,但是当它应该是 180 行时输出只有 40 行。更奇怪的是索引行索引仍然存在。它从第 0 行开始,然后跳到第 140 行。

我还尝试打印输出以检查“to_csv”函数期间是否存在错误,并且它输出相同的内容。

功能:

def df_difference(df1, df2, which=None):
# Find rows which are different between two DataFrames.
comparison_df = df1.merge(df2,
indicator=True,
how='outer')
if which is None:
diff_df = comparison_df[comparison_df['_merge'] != 'both']
else:
diff_df = comparison_df[comparison_df['_merge'] == which]
diff_df.to_csv('data/diff ' + now.strftime('%m-%d-%Y') + '.csv')
return diff_df

函数的使用:

df_compared = df_difference(df_check[['SS', 'F_DOD']], df_to_add_pc[['SS', 'F_DOD']])

当前输出:

,    SS,       EnteredDate,  F_DOD       ,_merge
0, 12, 2020-06-18 ,2020-06-01 ,left_only
140, 123,, 2020-05-19 ,right_only
141, 1234,, 2020-06-06 ,right_only
142, 12345,, 2020-06-06 ,right_only
143, 123456,, 2020-05-02 ,right_only
144, 11234,, 2020-05-23 ,right_only
145, 22134,, 2020-05-29 ,right_only
146, 123124,, 2020-05-22 ,right_only
147, 1234512,,2020-05-28 ,right_only
148, 521312,, 2020-05-17 ,right_only
149, 123412,, 2020-05-26 ,right_only
150, 1236231,,2020-05-25 ,right_only
151, 782123,, 2020-05-27 ,right_only
152, 7812312,,2020-05-17 ,right_only
153, 879122,, 2020-05-10 ,right_only
154, 981293,, 2020-06-05 ,right_only
155, 98712,, 2020-05-15 ,right_only
156, 867891,, 2020-05-11 ,right_only
157, 12312,, 2020-05-13 ,right_only
158, 123541,, 2020-05-29 ,right_only
159, 98712,, 2020-05-09 ,right_only
160, 908123,, 2020-06-04 ,right_only
161, 897291,, 2020-06-04 ,right_only
162, 8791231,,2020-06-01 ,right_only
163, 89712,, 2020-05-12 ,right_only
164, 9081,, 2020-05-17 ,right_only
165, 98712,, 2020-05-30 ,right_only
166, 0123,, 2020-06-06 ,right_only
167, 8213,, 2020-05-22 ,right_only
168, 9891,, 2020-05-19 ,right_only
169, 90812,, 2020-05-24 ,right_only
170, 908122,, 2020-06-01 ,right_only
171, 52131,, 2020-05-23 ,right_only
172, 34512,, 2020-05-09 ,right_only
173, 987891,, 2020-04-19 ,right_only
174, 908102,, 2020-05-05 ,right_only
175, 09521,, 2020-05-08 ,right_only
176, 82134,, 2020-05-16 ,right_only
177, 87182,, 2020-05-10 ,right_only
178, 76812,, 2020-05-28 ,right_only
179, 78921,, 2020-04-18 ,right_only
180, 89612,, 2020-04-17 ,right_only
181, 786123,, 2019-10-04 ,right_only

预期输出:寻找与当前输出相同的格式,但包括缺少的 100 多行。

最佳答案

为了查看整个输出,只需返回合并后的输出而不进行过滤:

def df_difference(df1, df2, which=None):
# Find rows which are different between two DataFrames.
comparison_df = df1.merge(df2,indicator=True,how='outer')
return comparison_df

关于python - 比较数据帧后,输出 CSV 未返回所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62923286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com