所以我有一个CSV文件,其中有两列我想比较
列相互对应,两列之间的每一相邻行都应遵循某种模式
例如:
A | B
1->;二,
3->;四,
1->;二,
3->;四,
每次a列中的行中有1时,B列中相邻行中应该有2
我想写一个脚本,如果有任何实例在a行中有1而在B行中没有2,它会提醒我
这就是我现在拥有的:
# Prep Work
#The Toolbox:
import numpy as np
import pandas as pd
import scipy as sp
from matplotlib import pyplot as plt
import seaborn as sns #plotting
import os #filepaths
import glob
import getpass # Login information
import math
import sys
unane = getpass.getuser()
#Paths:
path2proj = os.path.join('C:', os.sep, 'Users', unane, 'Documents', 'Mclean','Expts','PST', '')
path2data = os.path.join(path2proj,'Data','')
path2asys = os.path.join(path2proj,'Analysis', '')
path2figs = os.path.join(path2asys, 'figures', '')
path2hddm = os.path.join(path2asys, 'modeling', '')
extension = 'csv'
#Saved list of filrnames within "all_filenames"
#The glob module finds all the pathnames matching a specified pattern based on rules predetermined. Results are returned in arbitrary order
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames])
"Combined_csv.csv"
combined_csv.to_csv("combined_csv.csv",index=False, encoding='utf-8-sig')
df = pd.read_csv(path2data + 'combined_csv.csv')
df['left_stim_number'].equals(df['right_stim_number'])
我不确定我是否理解正确,但如果您只是想知道数据帧df中的行,其中列left_stim_number不等于right_stim_number,您可以简单地执行以下操作
编辑:上述内容显然不检查某些值。如果要检查左刺激号是否为1,然后右刺激号是否为2,可以执行以下操作:
如果我没弄错的话,B栏总是比A栏更容易理解。 在这种情况下,B=A+1
我建议您创建一个新列来存储a列和B列之间的比较结果,即,如果a和B满足条件,C=True,否则C=False。 这里df['check_stim_number']将是值为True或False的新列
相关问题 更多 >
编程相关推荐