简体   繁体   English

从 excel 行读取逗号分隔的数字 - python pandas

[英]reading comma separated numbers from excel row - python pandas

How to read excel row and check if the numbers of the first row are the same as the numbers of second row and third ( fourth, fifth, sixth and seventh row )?如何读取 excel 行并检查第一行的数字是否与第二行和第三行(第四、第五、第六和第七行)的数字相同? I would like to add user defined number of iterations that should be meet, before creating result.我想在创建结果之前添加用户定义的迭代次数。

I have just one column and 7 rows with 6 comma separated numbers.我只有一列和 7 行,有 6 个逗号分隔的数字。

This is example based on 2 iterations:这是基于 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

grouped numbers result ( from each row except first one ): 3,5分组数字结果(除第一行之外的每一行):3,5

Ideas?想法? E. E.

Given you have this file:鉴于您有此文件:

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

You could try this:你可以试试这个:

# 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'}

you can try this -你可以试试这个 -

Assuming you've a comma seperated csv file.假设您有一个逗号分隔的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM