差之间的独特要素排列的数据框

0

的问题

我有点新到这一点。 我目前正在试验数据的框架python和我有点粘的东西。 我需要让中列的数据框架,它具有同样差他们的独排序的要素。 我能够做到这一点的独立代码,但是我想这样做的动态让它从数据框架文件。

import numpy as np
import pandas as pd

first = [20, 10, 40, 30, 10]
sec = [94, 74, 34, 80]
df = pd.DataFrame([(first,sec) for first,sec in zip(first,sec)])
print(df)
cols = list(df.columns)
sorted_df = df.sort_values(by = cols, ascending = True)
print("sorted - \n", sorted_df)
all_unique = [sorted_df[col].unique() for col in cols]
print("UNIQUE:\n", all_unique)
diff = [np.diff(lst) for last in all_unique]
print("DIFF - \n", diff)

我能够得到列表中列出的差异。 现在我需要检查的所有元素的差异是相同的,如果是的话,那么必须返回列的名称,它首或秒。 输出我是:

   0   1
0  20  94
1  10  74
2  20  34
3  30  80
sorted - 
   0   1
0  20  94
1  10  74
2  20  30
3  30  80
UNIQUE:
[array([10, 20, 30]), array([74, 34, 94, 80])]
DIFF - 
[array([10, 10]), array([-40, 60, -14])]

在此之后,我应该返回的列名,或名单的名字具有相同的元素。 所需的输出应该清单的列名列其中有同样差别的排序的独特要素。 所以这里应该是:

output - ['first']
dataframe pandas python
2021-11-24 06:13:28
1

最好的答案

1

使用名单的理解与测试,如果排序的价值观现今是独一无二的:

#without unique values
output = [col for col in cols if df[col].sort_values().diff().nunique() == 1]
print("OUT - \n", output)
[0]

#with unique values
output = [col for col in cols 
          if df[col].drop_duplicates().sort_values().diff().nunique() == 1]

或者:

output = [col for col in cols if np.unique(np.diff(np.unique(df[col]))).shape[0] == 1]
print("OUT - \n", output)
[0]
2021-11-24 07:05:30

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................