Pee*_*eet 4 python dataframe python-3.x pandas
鉴于以下熊猫df:
import pandas as pd
df = pd.DataFrame({'1' : ['title1','R','R','R'],
'2' : ["title2", "NR" ,"NR", "NR"],
'3' : ["title3", "R" , "NR", "NR"],
'4' : ["title4", "R", "NR", "R"]})
Run Code Online (Sandbox Code Playgroud)
以及更长的字符串列表:
List = ['2633', 'title1', '3327', 'title2', '18', 'title3', '5', 'title4', '5835', 'title5', '394', 'title6']
Run Code Online (Sandbox Code Playgroud)
在python环境中是否有可能用字符串列表中每个对标题之前的数字替换df中的标题。
预期产量:
dfnew = pd.DataFrame({'1' : ['2633','R','R','R'],
'2' : ["3327", "NR" ,"NR", "NR"],
'3' : ["28", "R" , "NR", "NR"],
'4' : ["5", "R", "NR", "R"]})
dfnew
1 2 3 4
0 2633 3327 28 5
1 R NR R R
2 R NR NR NR
3 R NR NR R
Run Code Online (Sandbox Code Playgroud)
我认为a regex可以解决问题,但是我不知道如何从列表中访问正确的数字。
预先感谢您的每一个帮助!
dict从偶数和奇数索引创建一个键值对,并用于replace替换title为numbers:
d = {k:v for k,v in zip(List[1::2], List[::2])}
print(df.replace(d))
Run Code Online (Sandbox Code Playgroud)
输出:
1 2 3 4
0 2633 3327 18 5
1 R NR R R
2 R NR NR NR
3 R NR NR R
Run Code Online (Sandbox Code Playgroud)
说明
List[1::2] 将为您提供列表中奇数索引处的元素 ['title1', 'title2', 'title3', 'title4', 'title5', 'title6']
和
List[::2] 将为您提供列表中偶数索引处的元素 ['2633', '3327', '18', '5', '5835', '394']
我会做这样的事情:
import pandas as pd
df = pd.DataFrame({'1' : ['title1','R','R','R'],
'2' : ["title2", "NR" ,"NR", "NR"],
'3' : ["title3", "R" , "NR", "NR"],
'4' : ["title4", "R", "NR", "R"]})
List = ['2633', 'title1', '3327', 'title2', '18', 'title3', '5', 'title4', '5835', 'title5', '394', 'title6']
# mapping every title with its number
mydict = {}
for i in range(len(List)) :
if i %2 == 0 :
mydict[List[i+1]] = List[i]
print mydict
#>>>{'title1': '2633', 'title2': '3327', 'title3': '18', 'title4': '5', 'title5': '5835', 'title6': '394'}
for k in df :
title = df[k][0]
df[k][0] = mydict[title]
print df
#>>> 1 2 3 4
#>>>0 2633 3327 18 5
#>>>1 R NR R R
#>>>2 R NR NR NR
#>>>3 R NR NR R
Run Code Online (Sandbox Code Playgroud)