python – 替换部分匹配字符串的pandas数据框中的列名

python – 替换部分匹配字符串的pandas数据框中的列名,第1张

概述背景 我想在数据框中识别部分匹配字符串的列名称,并将其替换为原始名称以及添加到其中的一些新元素.新元素是由列表定义的整数.这是一个similar question,但我担心建议的解决方案在我的特定情况下不够灵活. here是另一篇文章,其中有几个很好的答案接近我面临的问题. 有些研究 我知道我可以组合两个字符串列表,使用字典作为函数df.rename中的输入将它们成对映射到into a dicti 背景

我想在数据框中识别部分匹配字符串的列名称,并将其替换为原始名称以及添加到其中的一些新元素.新元素是由列表定义的整数.这是一个similar question,但我担心建议的解决方案在我的特定情况下不够灵活. here是另一篇文章,其中有几个很好的答案接近我面临的问题.

有些研究

我知道我可以组合两个字符串列表,使用字典作为函数df.rename中的输入将它们成对映射到into a dictionary和rename the columns.但考虑到现有列的数量会有所不同,这似乎有点过于复杂,而且不够灵活.与要重命名的列数一样.

以下代码段将生成一个输入示例:

# librarIEsimport numpy as npimport pandas as pdimport itertools# A dataframeObservations = 5Columns = 5np.random.seed(123)df = pd.DataFrame(np.random.randint(90,110,size=(Observations,Columns)),columns = ['Price','obs_1','obs_2','obs_3','obs_4'])dateList = pd.date_range(pd.datetime.today().strftime('%Y-%m-%d'),periods=Observations).toList()df['Dates'] = dateListdf = df.set_index(['Dates'])print(df)

输入

我想识别以obs_开头的列名,并在=符号后面的列表newElements = [5,10,15,20]中添加元素(整数).名为Price的列保持不变.在obs_列之后出现的其他列也应保持不变.

以下代码段将演示所需的输出:

# Desired outputObservations = 5Columns = 5np.random.seed(123)df2 = pd.DataFrame(np.random.randint(90,'Obs_1 = 5','Obs_2 = 10','Obs_3 = 15','Obs_4 = 20'])df2['Dates'] = dateListdf2 = df2.set_index(['Dates'])print(df2)

产量

我的尝试

# define the partial string I'm lookin forstringMatch = 'Obs_'# Put existing column names in a Listoldnames = List(df)# Put elements that should be added to the column names# where the three first letters match 'obs_'newElements = [5,20]oldElements = [1,2,3,4]# Change types of the elements in the Liststr_newElements = [str(x) for x in newElements]str_oldElements = [str(y) for y in oldElements]str_newnames = str_newElements.copy()# Since I kNow the first column should not be renamed,# I start with 'Price' in a Listnewnames = ['Price']# Then I add the renamed parts to the same Listi = 0for oldElement in str_oldElements:       #print(repr(oldElement) + repr(str_newElements[i]))    newnames.append(stringMatch + oldElement + ' = ' + str_newElements[i])    i = i + 1# Rename columns using the dict as input in df.renamedf.rename(columns = dict(zip(oldnames,newnames)),inplace = True)print('My attempt: ',df)

已经完成了新列名的完整列表
我也可以使用df.columns = newnames,
但希望你们其中一个人有使用的建议
df.rename以更加pythonic的方式.

谢谢你的任何建议!

这是一个简单的复制粘贴的完整代码:

# librarIEsimport numpy as npimport pandas as pdimport itertools# A dataframeObservations = 5Columns = 5np.random.seed(123)df = pd.DataFrame(np.random.randint(90,periods=Observations).toList()df['Dates'] = dateListdf = df.set_index(['Dates'])print('input: ',df)# Desired outputObservations = 5Columns = 5np.random.seed(123)df2 = pd.DataFrame(np.random.randint(90,'Obs_4 = 20'])df2['Dates'] = dateListdf2 = df2.set_index(['Dates'])print('Desired output: ',df2)# My attempts# define the partial string I'm lookin forstringMatch = 'Obs_'# Put existing column names in a Listoldnames = List(df)# Put elements that should be added to the column names# where the three first letters match 'obs_'newElements = [5,4]# Change types of the elements in the Liststr_newElements = [str(x) for x in newElements]str_oldElements = [str(y) for y in oldElements]str_newnames = str_newElements.copy()# Since I kNow the first column should not be renamed,# I start with 'Price' in a Listnewnames = ['Price']# Then I add the renamed parts to the same Listi = 0for oldElement in str_oldElements:    #print(repr(oldElement) + repr(str_newElements[i]))    newnames.append(stringMatch + oldElement + ' = ' + str_newElements[i])    i = i + 1# Rename columns using the dict as input in df.renamedf.rename(columns = dict(zip(oldnames,df)

编辑:后果

仅仅一天之后,这么多好的答案真是太神奇了!这使得很难确定接受哪个答案.我不知道以下是否会给整个帖子增加很多价值,但我继续把所有建议都包含在函数中并用%timeit测试它们.

结果如下:

建议fram HH1是第一个发布的,也是执行时间最快的之一.如果有人感兴趣,我会在稍后提供代码.

编辑2

当我尝试时,来自suvy的建议呈现了这些结果:

该片段工作正常,直到最后一行.在运行df = df.rename(columns = dict(zip(names,renames))之后,数据框看起来像这样:

解决方法 这有用吗?

df.columns = [col + ' = ' + str(newElements.pop(0)) if col.startswith(stringMatch) else col for col in df.columns]
总结

以上是内存溢出为你收集整理的python – 替换部分匹配字符串的pandas数据框中的列名全部内容,希望文章能够帮你解决python – 替换部分匹配字符串的pandas数据框中的列名所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1194383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存