0

Here's the thing, I need to put one row from other dataframe to the top of main dataframe in pandas, above first row where are columns named.

Sample :

       1value  2value 3value 4value 5value
acity    4       3       6      2     6
bcity    2       6       6      4     1
ccity    5       11      53     6     3
dcity    5       1       4      6     3 
gcity    6       4       2      7     4

And the other sample:

1value 2value 3value 4value 5value
 2       5       2      6     3 

And now I need to add value of second sample to the top of first sample. Desired output:

         2       5       2      6     3 
      1value  2value 3value 4value 5value
acity    4       3       6      2     6
bcity    2       6       6      4     1
ccity    5       11      53     6     3
dcity    5       1       4      6     3 
gcity    6       4       2      7     4

And just for mention, I have about 3000 rows, and 250 columns in this Sample dataframe.

I don't have any code yet, I tried to find here something...

2
  • Are you looking for a multi-index column data frame? Commented Mar 24, 2017 at 16:59
  • I'm not really sure what you mean, I'm a beginner, but here it is. Values from second sample(that is dataframe too) are reference values and that's the reason why that should be above the first row, where are names of columns. @Psidom Commented Mar 24, 2017 at 17:04

1 Answer 1

1

Not sure if this is what you need, but a multi index data frame looks like the output:

df1 or second sample:

enter image description here

df or the first sample:

enter image description here

Rename the columns with a multi-index columns:

df.columns = pd.MultiIndex.from_arrays([df1.values[0], df.columns])

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, that's it, but I just have problem called : unhashable type: 'dict'
I can't reproduce the problem. Are the two samples both data frames?
Yes, both are data frames. Type of both is pandas.core.frame.DataFrame
You might have some issues somewhere else. I can't think of a way this can cause a unhashable error.
Okey, thank you anyway, this code is what I have look for. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.