简体   繁体   中英

In pandas, how do I insert a new row into a dataframe one column value at a time

I want to insert a new row into my dataframe, one value at a time, so I know exactly which values going into which column, don't judge me.

Here is what I have but printing it show empty dataframe. I am checking if date already exist to insert a new row or get the existing row for that date.

    if(trade["date"] in self.df["date"]):
        row = self.df[self.df.date == trade["date"]]
    else:
        row = self.df.append(pd.Series(), ignore_index=True)
    row["date"] = trade["date"]
    row["direction"] = trade["direction"]
    row["type"] = trade["type"]
    row["strategy"] = trade["strategy"]
    row["strike"] = trade["strike"]
    row["shortLeg"] = trade["shortLeg"]
    row["longLeg"] = trade["longLeg"]
    row["shortLeg_strike"] = trade["shortLeg_strike"]
    row["longLeg_strike"] = trade["longLeg_strike"]
    row["maxRisk"] = trade["maxRisk"]
    row["maxReturn"] = trade["maxReturn"]
    row["returnRatio"] = trade["returnRatio"]
    row["breakevenPrice"] = trade["breakevenPrice"]
    row["profitTargetPrice"] = trade["profitTargetPrice"]
    print(self.df)

Oh figure it out, stupid mistake, the row is not linked to the original dataframe.

self.df = row

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