使用列号而不是名称来填充多列

发布于 2025-01-16 13:00:43 字数 289 浏览 3 评论 0原文

我试图用固定数字填充 50 列数据帧的所有 NaN 值。有很多列以不同的名称使用它们,并且它们总是并排的。 我可以为此使用一系列列号吗? 而不是

df['column_name'] = df['column_name'].fillna(100)

有诸如

df[column 1: column 50] = df[column 1: column 50].fillna(100)

附加问题之类的问题:我可以轻松地找到数据框的列号和标题吗?

I am trying to fill all NaN values of a 50columns of dataframe with a fixed number. There are a lot of columns to be using them by theri names and they are always side by side.
Can I use a range of their column numbers for that?
Instead of

df['column_name'] = df['column_name'].fillna(100)

to have sothing like

df[column 1: column 50] = df[column 1: column 50].fillna(100)

additional question: can I easily spot the column number and the header of my dataframe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2025-01-23 13:00:45

鉴于您的众多问题,让我们举个例子:

from string import ascii_uppercase
import numpy as np
np.random.seed(0)
df = pd.DataFrame(np.random.choice([0, np.nan], size=(4,10)),
                  columns=list(ascii_uppercase[:10]))

     A    B    C    D    E    F    G    H    I    J
0  0.0  NaN  NaN  0.0  NaN  NaN  NaN  NaN  NaN  NaN
1  NaN  0.0  0.0  NaN  0.0  0.0  0.0  0.0  0.0  NaN
2  0.0  NaN  NaN  0.0  0.0  NaN  NaN  NaN  NaN  0.0
3  NaN  0.0  NaN  0.0  NaN  NaN  0.0  NaN  NaN  0.0

目标是填充第 1 至 5 列(包括):

     A      B      C      D      E      F    G    H    I    J
0  0.0  100.0  100.0    0.0  100.0  100.0  NaN  NaN  NaN  NaN
1  NaN    0.0    0.0  100.0    0.0    0.0  0.0  0.0  0.0  NaN
2  0.0  100.0  100.0    0.0    0.0  100.0  NaN  NaN  NaN  0.0
3  NaN    0.0  100.0    0.0  100.0  100.0  0.0  NaN  NaN  0.0
使用 iloc 仅在第 1 至 5 列(包括)中填充 NaN:
df.iloc[:,1:5+1] = df.iloc[:,1:5+1].fillna(100)
与名称 B- 相同>F 使用 loc
df.loc[:,'B':'F'] = df.loc[:,'B':'F'].fillna(100)
使用 loc 混合位置/标签索引:
last = df.columns[5]
df.loc[:,'B':last] = df.loc[:,'B':last].fillna(100)
使用 iloc 混合位置/标签索引:
last = df.columns.get_loc('F')+1
df.iloc[:,1:last] = df.iloc[:,1:last].fillna(100)

Given your numerous questions, lets get an example:

from string import ascii_uppercase
import numpy as np
np.random.seed(0)
df = pd.DataFrame(np.random.choice([0, np.nan], size=(4,10)),
                  columns=list(ascii_uppercase[:10]))

     A    B    C    D    E    F    G    H    I    J
0  0.0  NaN  NaN  0.0  NaN  NaN  NaN  NaN  NaN  NaN
1  NaN  0.0  0.0  NaN  0.0  0.0  0.0  0.0  0.0  NaN
2  0.0  NaN  NaN  0.0  0.0  NaN  NaN  NaN  NaN  0.0
3  NaN  0.0  NaN  0.0  NaN  NaN  0.0  NaN  NaN  0.0

The goal is to fill columns 1 to 5 (included):

     A      B      C      D      E      F    G    H    I    J
0  0.0  100.0  100.0    0.0  100.0  100.0  NaN  NaN  NaN  NaN
1  NaN    0.0    0.0  100.0    0.0    0.0  0.0  0.0  0.0  NaN
2  0.0  100.0  100.0    0.0    0.0  100.0  NaN  NaN  NaN  0.0
3  NaN    0.0  100.0    0.0  100.0  100.0  0.0  NaN  NaN  0.0
filling NaN only in columns 1 to 5 (included) using iloc:
df.iloc[:,1:5+1] = df.iloc[:,1:5+1].fillna(100)
same thing with names B->F using loc:
df.loc[:,'B':'F'] = df.loc[:,'B':'F'].fillna(100)
mixed position/label indexing using loc:
last = df.columns[5]
df.loc[:,'B':last] = df.loc[:,'B':last].fillna(100)
mixed position/label indexing using iloc:
last = df.columns.get_loc('F')+1
df.iloc[:,1:last] = df.iloc[:,1:last].fillna(100)
空名 2025-01-23 13:00:44

只需报告 mozway 在评论中正确建议的答案(全部归功于他)

解决方案只是

df.iloc[:,1:50] = df.iloc[:,1:50].fillna(100)

意味着您想要选择每行 : 和 1 到 50 1:50 之间的列。请注意,第二个索引上的选择是排他的。

Just reporting the answer that mozway correctly suggested in the comments (all creds to him)

The solution is simply

df.iloc[:,1:50] = df.iloc[:,1:50].fillna(100)

meaning that you want to select every row : and columns between 1 and 50 1:50. Beware that selection is exclusive on the second index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文