![](/img/trans.png)
[英]Python/Pandas: Converting numbers by comma separated for thousands
[英]reading comma separated numbers from excel row - python pandas
如何讀取 excel 行並檢查第一行的數字是否與第二行和第三行(第四、第五、第六和第七行)的數字相同? 我想在創建結果之前添加用戶定義的迭代次數。
這是基於 2 次迭代的示例:
column 1
1.) 1,2,3,4,5,6
2.) 1,3,5,7,9,10 ---> 1,3,5
3.) 3,5,7,9,10,11 ---> 3,5
分組數字結果(除第一行之外的每一行):3,5
想法? E.
鑒於您有此文件:
df = pd.read_excel(io='path_to_file.xlsx', header=None)
print(df)
# Output:
0
0 1, 2, 3, 4, 5, 6
1 1, 3, 5, 7, 9, 10
2 3, 5, 7, 9, 10, 11
你可以試試這個:
# Setup
previous_row = df.iloc[0]
user_number_of_iterations = 4 # for instance
items = []
# Iterate over dataframe rows and find common values
# between row[n] and row[n-1]
for i, row in df.iterrows():
if i == 0:
continue
if i > user_number_of_iterations:
break
a = set(sorted(row.values[0].split(", ")))
b = set(sorted(previous_row.values[0].split(", ")))
items.append(a & b)
previous_row = row
# Get symmetric difference between two sets
print(f"Missed numbers at iteration {i}: {a ^ b}")
# Find common values between previous results
result = items[0]
for item in items[1:]:
result = set(result) & set(item)
print(sorted(result)) # Output: ['3', '5']
# Missed numbers at iteration 1: {'7', '4', '2', '10', '6', '9'}
# Missed numbers at iteration 2: {'11', '1'}
你可以試試這個 -
假設您有一個逗號分隔的 csv 文件。
from functools import reduce
result = reduce(lambda x,y: x.intersection(y), df.apply(set, axis=1).values) # {3,5}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.