简体   繁体   English

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

[英]How to append to individual columns in a Pandas DataFrame

So I want to add/append data to a specific pandas dataFrame column but without it causing NaN values in the remaining columns所以我想将数据添加/附加到特定的 Pandas dataFrame 列,但不会导致剩余列中的 NaN 值

Ie IE

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)

This will 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

What we want 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

I know I can in this case use one .append for all the different columns.我知道在这种情况下我可以对所有不同的列使用一个 .append 。 But I have some cases where the data to be appended will vary based on multiple conditions.但是在某些情况下,要附加的数据会因多种条件而异。 Hence I'd like to know if it's possible to append to single columns in a dataframe without producing NaN values in the remaining columns.因此,我想知道是否可以附加到数据框中的单列而不在其余列中生成 NaN 值。 So that I can avoid writing hundreds of if else statements.这样我就可以避免编写数百个 if else 语句。

Or if someone has any good idea regarding how to 'collapse' the NaN values (removing the NaN values without removing the entire row so that if there is a NaN value at index 0 in column 3 and there is a integer 5 at index 1 in the same column the integer 5 gets moved up to index 0)或者,如果有人对如何“折叠” NaN 值有任何好主意(删除 NaN 值而不删除整行,以便如果在第 3 列的索引 0 处有一个 NaN 值并且在索引 1 处有一个整数 5整数 5 向上移动到索引 0 的同一列)

Happy to hear any ideas.很高兴听到任何想法。

IIUC for your current example you can try this: IIUC 对于您当前的示例,您可以尝试以下操作:

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

Output:输出:

 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

then remove the NaN :然后删除NaN

DataFrame.dropna(inplace=True)

Outpt:输出:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM