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

Question:

I had two lists

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

I want to make something like this:

             PREC            TEMP
0            98               42 
Asked By: Lucas Medeiros

||

Answers:

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
Answered By: Abhi
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
Answered By: Bhargav

you can also use this,

import pandas as pd

values = ['98', '42']
columns = ['PREC', 'TEMP']
df = pd.DataFrame(dict(zip(columns,values)),index=[0])

the output is,

  PREC TEMP
0   98   42
Answered By: Berlin Benilo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.