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
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
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
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
I had two lists
values = ['98', '42']
columns = ['PREC', 'TEMP']
I want to make something like this:
PREC TEMP
0 98 42
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
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
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