I have a variable called ‘Specimen’ from a dataframe imported from a xlsx file, and I want to implement the result of this variable (in this case 25) in a new dataframe?
What I have:
JavaScript
x
3
1
Specimen = np.array(df.loc['Specimen', ['x1']])
2
Specimen
3
array([[25.0]], dtype=object)
JavaScript
1
5
1
New_Df = pd.DataFrame(Variable_list)
2
New_Df['Result_list'] = Result_list
3
New_Df.columns =['Name', 'Result']
4
New_Df
5
JavaScript
1
6
1
Name Result
2
0 L_TotalNOT 52.541091
3
1 R_TotalNOT 40.139543
4
2 L_TotalGON 59.271545
5
3 R_TotalGON 63.784038
6
What I want:
JavaScript
1
6
1
25 Name Result
2
0 L_TotalNOT 52.541091
3
1 R_TotalNOT 40.139543
4
2 L_TotalGON 59.271545
5
3 R_TotalGON 63.784038
6
I have tried
JavaScript
1
2
1
Output.index.names = [Specimen]
2
But this gives the following error: TypeError: RangeIndex.name must be a hashable type
ps: I’m quite new here, so I hope I’m asking thing the right way, Many thanks in advance
Advertisement
Answer
I feel like I may be missing a requirement from your question but here is what I think might work for you:
Set the variable to match yours:
JavaScript
1
8
1
Specimen = np.array([[25.0]], dtype=object)
2
Specimen
3
4
array([[25.0]], dtype=object)
5
6
Specimen[0][0]
7
25.0
8
Then rename the index:
JavaScript
1
9
1
new_df.index.rename(Specimen[0][0], inplace=True)
2
3
Name Result
4
25.0
5
0 L_TotalNOT 52.541091
6
1 R_TotalNOT 40.139543
7
2 L_TotalGON 59.271545
8
3 R_TotalGON 63.784038
9
The index has been renamed:
JavaScript
1
3
1
new_df.index
2
Int64Index([0, 1, 2, 3], dtype='int64', name=25.0)
3