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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:57:12  来源:igfitidea点击:

How to append a row to another dataframe

pythonpandasdataframeappend

提问by Ironman10

I have dataframes df1and df2both with columns ["Ticker", "Adj.Factor", "Date"]. I want to add to df2the complete row from df1if the value of "Adj.Factor" in that row in df1equals 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的末尾,返回一个新对象。

REF

参考文献