[英]How to create a new column and insert value at a particular row in pandas dataframe?
[英]In pandas, how do I insert a new row into a dataframe one column value at a time
我想在我的 dataframe 中插入一個新行,一次一個值,所以我確切地知道哪些值進入了哪一列,不要評判我。
這是我所擁有的,但打印它顯示空的 dataframe。 我正在檢查日期是否已經存在以插入新行或獲取該日期的現有行。
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)
哦弄清楚了,愚蠢的錯誤,該行未鏈接到原始dataframe。
self.df = row
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.