如何附加到 Pandas DataFrame 中的各个列

How to append to individual columns in a Pandas DataFrame

所以我想 add/append 数据到特定的 pandas dataFrame 列,但不会在其余列中导致 NaN 值

DataFrame = pd.DataFrame(columns=["column1", "column2", "column3"])
for i in range():
    DataFrame = DataFrame.append({"column1":int(i)}, ignore_index=True)
    DataFrame = DataFrame.append({"column2":float(i*2)}, ignore_index=True)
    DataFrame = DataFrame.append({"column3":int(i*5)}, ignore_index=True)
print(DataFrame)

这将 return:

   column1  column2  column3
0      0.0      NaN      NaN
1      NaN      0.0      NaN
2      NaN      NaN      0.0
3      1.0      NaN      NaN
4      NaN      2.0      NaN
5      NaN      NaN      5.0
6      2.0      NaN      NaN
7      NaN      4.0      NaN
8      NaN      NaN     10.0

我们想要什么returned:

   column1  column2  column3
0      0.0      0.0      0.0
1      1.0      2.0      5.0
2      2.0      4.0     10.0

我知道在这种情况下我可以对所有不同的列使用一个 .append。但在某些情况下,要附加的数据会因多种条件而异。因此,我想知道是否可以追加到数据框中的单个列而不在其余列中生成 NaN 值。这样我就可以避免编写数百个 if else 语句。

或者如果有人对如何 'collapse' NaN 值有任何好主意(删除 NaN 值而不删除整行,这样如果第 3 列的索引 0 处有一个 NaN 值并且有同一列中索引 1 处的整数 5 整数 5 向上移动到索引 0)

很高兴听到任何想法。

IIUC 对于您当前的示例,您可以试试这个:

DataFrame[['column2','column3']]=DataFrame[['column2','column3']].bfill()

输出:

 column1  column2   column3
0   0.0     0.0     0.0
1   NaN     0.0     0.0
2   NaN     2.0     0.0
3   1.0     2.0     5.0
4   NaN     2.0     5.0
5   NaN     4.0     5.0
6   2.0     4.0     10.0
7   NaN     4.0     10.0
8   NaN     6.0     10.0
9   3.0     6.0     15.0
10  NaN     6.0     15.0
11  NaN     8.0     15.0
12  4.0     8.0     20.0
13  NaN     8.0     20.0
14  NaN     NaN     20.0

然后删除 NaN :

DataFrame.dropna(inplace=True)

输出:

 column1  column2   column3
0   0.0     0.0     0.0
3   1.0     2.0     5.0
6   2.0     4.0     10.0
9   3.0     6.0     15.0
12  4.0     8.0     20.0