简体   繁体   中英

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.

For example, If CSV file contains columns like date, value, product. Then, I need to get all 3 data between sdate(Provided by a user) and edate(Provided by a user).

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

Here, sdate and edate are variable name and Date is column name in csv file.

As shown in code that data are read from CSV file and for applying conditions on same column inner for loop is used. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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