使用列号而不是名称来填充多列
我试图用固定数字填充 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您的众多问题,让我们举个例子:
目标是填充第 1 至 5 列(包括):
使用 iloc 仅在第 1 至 5 列(包括)中填充 NaN:
与名称 B- 相同>F 使用
loc
:使用
loc
混合位置/标签索引:使用
iloc
混合位置/标签索引:Given your numerous questions, lets get an example:
The goal is to fill columns 1 to 5 (included):
filling NaN only in columns 1 to 5 (included) using
iloc
:same thing with names B->F using
loc
:mixed position/label indexing using
loc
:mixed position/label indexing using
iloc
:只需报告 mozway 在评论中正确建议的答案(全部归功于他)
解决方案只是
意味着您想要选择每行
:
和 1 到 501:50
之间的列。请注意,第二个索引上的选择是排他的。Just reporting the answer that mozway correctly suggested in the comments (all creds to him)
The solution is simply
meaning that you want to select every row
:
and columns between 1 and 501:50
. Beware that selection is exclusive on the second index.