使用另一个数据帧中的值重命名数据框的列

时间:2018-04-23 13:36:15

标签: python-2.7 pandas dataframe

所以我有一个大致如下的数据框:

name1   name2   name3
123     456     678
123     456     678
123     456     678

和另一个看起来像这样的数据框

name2  abc
name3  cdf
name1  fgh

有没有办法让第一个数据帧列名如下:

fgh     abc     cdf
123     456     678
123     456     678
123     456     678

感谢。

2 个答案:

答案 0 :(得分:1)

使用rename Series index A print (df2) A B 0 name2 abc 1 name3 cdf 2 name1 fgh df1 = df1.rename(columns=df2.set_index('A')['B']) print (df1) fgh abc cdf 0 123 456 678 1 123 456 678 2 123 456 678 print (df2.set_index('A')['B']) A name2 abc name3 cdf name1 fgh Name: B, dtype: object

dictionary

<强>详细

zip

df1 = df1.rename(columns=dict(zip(df2.A, df2.B))) 创建的print (dict(zip(df2.A, df2.B))) {'name3': 'cdf', 'name1': 'fgh', 'name2': 'abc'}

<ComboBox x:Name="ImportDate" 
        DisplayMemberPath="FileDate" 
        SelectedValuePath="ID" 
        ItemsSource="{Binding Mode=OneWay}" 
        SelectedIndex="0" 
        Style="{DynamicResource sanComboBox_Standard}" />

<强>详细

<html>
<body>
<form>
function getinput() 
{ 
index ++; 
var singleRecord = ""; 
**singleRecord += " Employee_id : <input type='text' name='txt_Employee_id"+index+"'>";** //here employee_id should fetch dynamically from other jsp page
singleRecord += " Project_name : <input type='text' name='txt_project_name"+index+"'>"; 
singleRecord += "Project/Task : <input type='text' name='txt_Header"+index+"'>"; 
singleRecord += "Department : <input type='text' name='txt_Department"+index+"'>"; 
singleRecord += "Description : <input type='text' name='txt_description"+index+"'>"; 
<!--singleRecord+="Date : <input type='date'  name='datainsertdate"+index+"'>"; --->
singleRecord += "<hr/>"; 
$("#inputs").append(singleRecord); 
$("#count").val(index); 
}

</script> 

</head> 

<body background="123-slideshow\blue_background.jpg">
<form action="EmployeeVendorValidation.jsp" method="post"> 

     <br><br><br><br>    
<input type="hidden" name="count" id="count"/> 



<div id="inputs"> 
</div> 
<button type="button" onclick="getinput()" class="button button5">Add</button> 

&nbsp; 
<button type="submit" class="button button2">Submit</button> 


</form> 
</body> 

</html>

答案 1 :(得分:0)

您可以使用Series get并将其分配回来

df.columns=s.get(df.columns)
df
Out[223]: 
s1  fgh  abc  cdf
0   123  456  678
1   123  456  678
2   123  456  678