我是一个用提供给我的代码对一些分类变量进行热编码的人。这行添加了一列0和1,名称的格式为prefix_categoricalValue
dataframe = pandas.concat([dataframe,pandas.get_dummies(dataframe[0], prefix='protocol')],axis=1).drop([0],axis=1)
我希望列的名称是它的索引,而不是prefix_categoricalValue
。在
我知道我可以做一些类似df.rename(columns={'prefix_categoricalValue': '0'}, inplace=True)
的操作,但我不确定如何对所有具有该前缀的列执行此操作。在
这是数据帧的一部分的示例。不管我是否决定保留本地的ė地址前缀,每个类别都会有它的名称。可以用索引重命名列吗?在
编辑:
我正在努力做到:
^{pr2}$但我不太清楚为什么它不起作用
IIUC公司
假设你的前缀是
local_address_0.0.0.0
。以下代码根据列在dataframe中的出现顺序,将以指定前缀开头的列重命名为列所具有的索引:这将在控制台中显示警告:
^{pr2}$但这只是一个警告,dataframe的列名会更新。如果要了解有关警告的详细信息,请参阅How to deal with SettingWithCopyWarning in Pandas?
如果有人知道如何在没有警告的情况下做同样的事情,请评论。在
相关问题 更多 >
编程相关推荐