Home > Software engineering >  How to combine two lists to dataframe, where one of them will be my columns and another my values?
How to combine two lists to dataframe, where one of them will be my columns and another my values?

Time:10-14

I had two lists

values = ['98', '42']
columns = ['PREC', 'TEMP']

I want to make something like this:

             PREC            TEMP
0            98               42 

CodePudding user response:

The below would work where you pass the values to convert it to dataframe and then use the columns params to assign columns names and then you can override the index with your required index values in the iteration

import pandas as pd
values = ['98', '42']
my_columns = ['PREC', 'TEMP']
my_index = '27/12/2021'

df = pd.DataFrame([values], columns=[my_columns])
df.rename(index={0:my_index},inplace=True)

           PREC    TEMP
27/12/2021  98      42

CodePudding user response:

import pandas as pd

values = ['98', '42']
columnss = ['PREC', 'TEMP']

df = pd.DataFrame([values])
df.columns =columnss
print(df)

output

 PREC TEMP
0   98   42
  • Related