简体   繁体   English

使用python从csv文件的一列中应用2个条件来获取多列

[英]Get multiple columns by applying 2 conditions on one column from csv file using python

I am trying to get data from CSV file by applying two conditions on the same column. 我试图通过在同一列上应用两个条件从CSV文件获取数据。

For example, If CSV file contains columns like date, value, product. 例如,如果CSV文件包含日期,值,产品等列。 Then, I need to get all 3 data between sdate(Provided by a user) and edate(Provided by a user). 然后,我需要获取sdate(由用户提供)和edate(由用户提供)之间的所有3个数据。

df.loc[(df['Date'] == sdate) & (df['Date'] == edate)]

Here, sdate and edate are variable name and Date is column name in csv file. 此处,sdate和edate是csv文件中的变量名,而Date是列名。

As shown in code that data are read from CSV file and for applying conditions on same column inner for loop is used. 如代码中所示,从CSV文件中读取数据,并将条件应用于同一列内部的for循环。 This will extract data with all rows according to condition given. 这将根据给定的条件提取所有行的数据。

with open(fName,'rt') as csvfile:
    data = list(csv.reader(csvfile))
    print(data)
    df = pd.read_csv(fName)
    print(df.loc[(df['Date'] == sdate)])
    for x in df['Date']:
    if (x<sdate) & (x>edate):
    df1 = df(x)
    print(df1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python CSV,使用CSV将多列合并为一列 - Python CSV, Combining multiple columns into one column using CSV 使用 Python 3.8 将一个 CSV 文件中的一列(向量)与另一个 CSV 文件中的两列(向量和数组)进行比较 - Compare one column (vector) from one CSV file with two columns (vector and array) from another CSV file using Python 3.8 将多个文本文件中的列与 csv 列文件 python 进行比较 - compare columns from multiple text files, to csv column file python 使用Python将列从一个.csv添加到另一个.csv文件 - Add column from one .csv to another .csv file using Python 从一列 Python 中包含多个值的 .CSV 文件中读取 - Reading from a .CSV file that contains multiple values in one column Python 从Python的CSV文件中仅获取一列 - Get only one column from CSV file in Python 通过更改标题将.csv文件中的单个列拆分为多个列,并使用Python 2将其保存到新的.csv文件中 - Splitting a single column in a .csv file into multiple columns with changes in headings and saving it in a new .csv file using Python 2 使用 Python 在 csv 文件中从字符串搜索中打印多列 - Print multiple columns from string search in csv file using Python 需要从多个 csv 文件中选择“第二列”并将所有“第二列”保存在一个 csv 文件中 - Need to pick 'second column' from multiple csv files and save all 'second columns' in one csv file 如何通过在python中的某些列上应用条件来过滤csv数据 - How to filter csv data by applying conditions on certain columns in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM