0

I have two dataframes. Data and metadata.

A reproducible example:

Data has 32 column names as X1, X2, etc.

{'X1': {0: '11.89%', 1: '10.71%'},
'X2': {0: 54734.0, 1: 55742.0},
'X3': {0: 80364.0, 1: 114426.0}}

Metadata has the same X1, X2.

{'Variable': {0: 'X1',
1: 'X2',
2: 'X3'},
'Definition': {0: 'Test',
1: 'Test',
2: 'Test'}}

My desired output - match rows by X1, X2 etc and have metadata Definition column as names of my data.

So far I tried to do the following:

# Drop X1, X2, X3 for from metadata
del metadata['Variable']
# Hardcode column names without matching
loan_data.columns = metadata

It gives me an output, but I need to clean it additionally with regex

[('Column1 text',),
('Column2 text.',),
('Column3 text.',),

I am sure there is a more elegant way. Thanks for tips!

UPDATED

df1 = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=['X1', 'X2'])

df2 = pd.DataFrame([['X1', 'Definition1'], ['X2', 'Definition1']], columns=['Variable', 'Definition'])

df_output = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=['Definition1', 'Definition2'])

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • 1
    Can't you simplify your example data in a digestible size? It's about describing your problem in a simple way, not showing your entire data. Besides that, a dictionary is not the ideal way to visualize your data. – Erfan Jul 28 '20 at 17:44
  • @Erfan, apologize, I made it shorter. I use dictionaries as `dput` in R. Happy to share in a different format, if you can give me a tip. Thanks! – Anakin Skywalker Jul 28 '20 at 17:48
  • 1
    Can you show your expected output in a dataframe (table) format? Right now its not clear what you mean with _"match rows by X1, X2 etc and have metadata Definition column as names of my data."_ – Erfan Jul 28 '20 at 17:53
  • 1
    Read more [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Erfan Jul 28 '20 at 17:54
  • @Erfan, done, thanks a lot for your time! – Anakin Skywalker Jul 28 '20 at 18:05

0 Answers0