检查 DF 中的列组合到 return 个唯一行
check combinations of columns in a DF to return unique rows
for a, b in itertools.combinations(number_of_notes_cols, 2):
weekly_meetings_difference = all_meetings_data[(all_meetings_data[a] != all_meetings_data[b]) == True]
上面的代码曾经有效:它将 return weekly_meetings_difference 的列对的所有组合的所有行,其中列值(如果这对任何一对都是正确的)列)。现在,returning weekly_meetings_difference 给了我一些(但不是全部)列值发生变化的行。
使用一些代码进行编辑:
之前(当一切似乎都运行良好时):
Number of Notes 03112016 Number of Notes 03192016 Number of Notes 03272016 Number of Notes 04042016
Meeting Name
X 12.0 NaN NaN NaN
Y 5.0 5.0 NaN NaN
Z 2.0 NaN NaN NaN
W NaN 6.0 713.0 740.0
之后(现在我已经更新了我需要信息的原始数据框):
Number of Notes 03112016 Number of Notes 03192016 Number of Notes 03272016 Number of Notes 04042016 Number of Notes 04122016 Emails 04122016
Meeting Name
A 37.0 37.0 38.0 38.0 37.0
X 12.0 NaN NaN NaN NaN NaN
Y 5.0 5.0 NaN NaN NaN NaN
Z 2.0 NaN NaN NaN NaN NaN
现在我已经完成了这个编辑,我注意到在将额外的列添加到数据框之后添加了行 A,并且删除了行 W(它们每次都应该显示)
首先,让我确定我理解这个问题。您是否在数据框中寻找具有多个唯一值的行?也就是说,该值在该行中至少更改一次。
import pandas as pd
df = pd.DataFrame({'a': [1, 1, 1], 'b': [1, 2, 3], 'c': [1, 1, 3]})
a b c
0| 1 1 1
1| 1 2 1
2| 1 3 3
在上面的数据框中,您需要第 1 行和第 2 行。如果是这样,我会这样做:
df.apply(pd.Series.nunique, axis=1)
其中 returns 数据帧每一行中唯一值的数量:
0 1
1 2
2 2
dtype: int64
使用该结果,我们可以 select 我们关心的行:
df[df.apply(pd.Series.nunique, axis=1) > 1]
returns 预期:
a b c
1| 1 2 1
2| 1 3 3
这是您想要的,还是别的什么?如果您澄清,很高兴进行编辑。
for a, b in itertools.combinations(number_of_notes_cols, 2):
weekly_meetings_difference = all_meetings_data[(all_meetings_data[a] != all_meetings_data[b]) == True]
上面的代码曾经有效:它将 return weekly_meetings_difference 的列对的所有组合的所有行,其中列值(如果这对任何一对都是正确的)列)。现在,returning weekly_meetings_difference 给了我一些(但不是全部)列值发生变化的行。
使用一些代码进行编辑:
之前(当一切似乎都运行良好时):
Number of Notes 03112016 Number of Notes 03192016 Number of Notes 03272016 Number of Notes 04042016
Meeting Name
X 12.0 NaN NaN NaN
Y 5.0 5.0 NaN NaN
Z 2.0 NaN NaN NaN
W NaN 6.0 713.0 740.0
之后(现在我已经更新了我需要信息的原始数据框):
Number of Notes 03112016 Number of Notes 03192016 Number of Notes 03272016 Number of Notes 04042016 Number of Notes 04122016 Emails 04122016
Meeting Name
A 37.0 37.0 38.0 38.0 37.0
X 12.0 NaN NaN NaN NaN NaN
Y 5.0 5.0 NaN NaN NaN NaN
Z 2.0 NaN NaN NaN NaN NaN
现在我已经完成了这个编辑,我注意到在将额外的列添加到数据框之后添加了行 A,并且删除了行 W(它们每次都应该显示)
首先,让我确定我理解这个问题。您是否在数据框中寻找具有多个唯一值的行?也就是说,该值在该行中至少更改一次。
import pandas as pd
df = pd.DataFrame({'a': [1, 1, 1], 'b': [1, 2, 3], 'c': [1, 1, 3]})
a b c
0| 1 1 1
1| 1 2 1
2| 1 3 3
在上面的数据框中,您需要第 1 行和第 2 行。如果是这样,我会这样做:
df.apply(pd.Series.nunique, axis=1)
其中 returns 数据帧每一行中唯一值的数量:
0 1
1 2
2 2
dtype: int64
使用该结果,我们可以 select 我们关心的行:
df[df.apply(pd.Series.nunique, axis=1) > 1]
returns 预期:
a b c
1| 1 2 1
2| 1 3 3
这是您想要的,还是别的什么?如果您澄清,很高兴进行编辑。