pandas 如何将一行附加到另一个数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47963548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append a row to another dataframe
提问by Ironman10
I have dataframes df1
and df2
both with columns ["Ticker", "Adj.Factor", "Date"]
. I want to add to df2
the complete row from df1
if the value of "Adj.Factor" in that row in df1
equals to 0
.
我有数据框,df1
并且df2
都带有 column ["Ticker", "Adj.Factor", "Date"]
。我想添加到df2
从完整的排df1
在该行中“Adj.Factor”如果值df1
等于0
。
I have the following code.
我有以下代码。
for x in range(tot_len):
if df1.iloc[x]['Adj.Factor'] == 0:
df2.append(df1.iloc[x]) --> not working.
`
`
I have tried printing the values and it shows the correct output. But the values are not appended to df2.
我试过打印这些值,它显示了正确的输出。但是这些值没有附加到 df2。
回答by tarashypka
It seems you missed an assignment. Here is a simpler solution
看来你错过了一项任务。这是一个更简单的解决方案
df2 = df2.append(df1[df1['Adj.Factor'] == 0])
回答by Hamed
Try:
尝试:
df2 = df2.append(df1.iloc[x])
DataFrame.append(self, other, ignore_index=False, verify_integrity=False, sort=False) → 'DataFrame
Append rows of otherto the end of caller, returning a new object.
DataFrame.append(self, other, ignore_index=False, verify_integrity=False, sort=False) → 'DataFrame
将other 的行附加到caller的末尾,返回一个新对象。