Reputation: 7
I am looking to extract columns from a dataframe using another dataframe. I don't want to hard code the column headers into the code as the data comes from a csv and columns can be added with different headers. Tried with .loc and using iterations, but nothing seems to work. This is what I have for code which are all from two different csv documents...
df1 =
Date a b c d
11/11/2011 2 3 4 5
11/12/2011 3 4 5 6
11/13/2011 4 5 6 7
df1 was translated using a .pivot and a,b,c,d's column header is Symbol and
df2=
Symbol
b
d
The expect outcome is...
b d
x 3 5
y 4 6
z 5 7
Tried doing
df1.loc(df2)
but got a 'DataFrame' objects are mutable, thus they cannot be hashed error
Thanks for the help!
Upvotes: 0
Views: 932
Reputation: 34086
You can do this:
In [157]: df1[df2['Symbol']]
Out[157]:
b d
0 3 5
1 4 6
2 5 7
Upvotes: 2