0

i want to Create a function that takes in two lists named keys and values as arguments and return a dataframe , example:create_dataframe(["One", "Two"], [["X", "Y"], ["A", "B"]]) -> should return a dataframe

   One Two
    
    0  X   A
    
    1  Y   B

for this purpose i have come with this below code so far(i am learning), but the result is only showing Zero, could anybody please guide me where i am wrong?

import pandas as pd
    def create_dataframe(keys,vlaues):
        keys = []
        values = []
        series = pd.Series(keys,index = values)
        df = pd.DataFrame(series)
        return df.T
    create_dataframe(["One", "Two"], [["X", "Y"], ["A", "B"]])
RAVI KUMAR
  • 386
  • 1
  • 12
  • ``pd.DataFrame(columns=keys, data = values)`` – sushanth Aug 20 '20 at 06:51
  • Does this answer your question? [Python Pandas Data frame creation](https://stackoverflow.com/questions/46562479/python-pandas-data-frame-creation) – sushanth Aug 20 '20 at 06:51
  • Hi , yes it certainly helped me to get a better understanding(as i am going through an online course) ,and the below code solved my problem – RAVI KUMAR Aug 20 '20 at 07:00

1 Answers1

0

Use DataFrame constructor with transpose:

def create_dataframe(keys,vlaues):
    return pd.DataFrame(vlaues, index=keys).T

df = create_dataframe(["One", "Two"], [["X", "Y"], ["A", "B"]])
print (df)
  One Two
0   X   A
1   Y   B
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252