python - Pandas: Use multiple columns of a dataframe as index of another -
I have found a large data frame with my data in it, and the second data frame of the same first dimension in which each Metadata is periodically (for example, what was the trial number, what kind of lawsuit it was).
What I have to do is to split large dataframes using the values of "MetadataFrame". I want to put these separately (rather than accumulating metadataaframes as a large one's multi index).
Right now, I'm trying to do this:
def my_func (container): container.big_df.set_index (['' 1 ',' However, it returns the following error: Code> ValueError: Only Boolean values should have passed the DataFrame
Note that this works fine, if I only pass a single column at set_index.
Can anyone also understand what is going wrong here? Alternatively, can someone tell me that I am doing this completely and stupidly, and there is a better way to go about this? :)
My solution
Thanks for the thoughts I played a bit with indexing, and it seems the easiest / fastest I did not like to strip the index of my name, and the values were difficult to cross seemed to.
dfa.set_index (dfb [['col1', 'col2']]) does not work, but dfa.set_index ([dfb.col1, dfb.col2])
does.
So, you can basically set dfb in a list of columns, with the following relationships, set_indix can work: [col1 ',' col2 ']]
Use MultiIndex.from_arrays ()
to create an index object :
pandas pd df1 = pd.DataFrame ({"A": [1,2,3], "B": ["a", "B", "c" ]}) Df2 = pd.DataFrame ({"C": [100,200,300]}) df2.index = pd.MultiIndex.from_arrays (df1.values.T) print df2
Result:
C1 one 100 2B 200 3C300
Comments
Post a Comment