简体   繁体   中英

saving output from for loop groupby condition

I referred multiple site related to below issue and I tried different ways but did not get the answer.

I took sample data and run the groupby with condition, I am getting correct output but when append to dataframe or list I am not getting same output which i see on jupter.

My code:

rd = pd.read_csv('C:/......Desktop\convdata.csv')

for x, y in rd.groupby(["ciq", "Obl"]):
    print(y.Date.duplicated())

After run the above code I am getting correct output:

在此处输入图像描述

But when I create empty data frame and append the duplicated to it, for that I wrote below code:

df3 = pd.DataFrame()

for x, y in rd.groupby(["ciq", "Obl"]):
    value = y.Date.duplicated()
    df3 = df3.append(value)

print(df3)

I am getting below output:

在此处输入图像描述

However, I want output similar like print(y.Date.duplicate) in df3.

Finally I got the answer:

pd.DataFrame(rd.groupby(["ciq", "Obl"]).apply(lambda x: x.Date).duplicated()).to_csv("finaltry.csv")

output looks like in csv file:

在此处输入图像描述

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