How do I add a list which has headers to a Dataframe, if I do it directly using pl.DataFrame(list)
, it creates the headers for me and keeps everything as a string. And also transposes the list, whereby the first element in my list becomes the first column in the df.
My list:
<code>[['Earnings estimate', 'Current qtr. (Jun 2024)', 'Next qtr. (Sep 2024)', 'Current year (2024)', 'Next year (2025)'], ['No. of analysts', '13', '11', '26', '26'], ['Avg. Estimate', '1.52', '1.62', '6.27', '7.23'], ['Low estimate', '1.36', '1.3', '5.02', '5.88'], ['High estimate', '1.61', '1.74', '6.66', '8.56'], ['Year ago EPS', '1.76', '1.36', '5.74', '6.27']]
</code>
<code>[['Earnings estimate', 'Current qtr. (Jun 2024)', 'Next qtr. (Sep 2024)', 'Current year (2024)', 'Next year (2025)'], ['No. of analysts', '13', '11', '26', '26'], ['Avg. Estimate', '1.52', '1.62', '6.27', '7.23'], ['Low estimate', '1.36', '1.3', '5.02', '5.88'], ['High estimate', '1.61', '1.74', '6.66', '8.56'], ['Year ago EPS', '1.76', '1.36', '5.74', '6.27']]
</code>
[['Earnings estimate', 'Current qtr. (Jun 2024)', 'Next qtr. (Sep 2024)', 'Current year (2024)', 'Next year (2025)'], ['No. of analysts', '13', '11', '26', '26'], ['Avg. Estimate', '1.52', '1.62', '6.27', '7.23'], ['Low estimate', '1.36', '1.3', '5.02', '5.88'], ['High estimate', '1.61', '1.74', '6.66', '8.56'], ['Year ago EPS', '1.76', '1.36', '5.74', '6.27']]
Desired output;
<code>import pandas as pd
data = [
['Earnings estimate', 'Current qtr. (Jun 2024)', 'Next qtr. (Sep 2024)', 'Current year (2024)', 'Next year (2025)'],
['No. of analysts', '13', '11', '26', '26'],
['Avg. Estimate', '1.52', '1.62', '6.27', '7.23'],
['Low estimate', '1.36', '1.3', '5.02', '5.88'],
['High estimate', '1.61', '1.74', '6.66', '8.56'],
['Year ago EPS', '1.76', '1.36', '5.74', '6.27']
]
headers = data[0]
rows = data[1:]
df = pd.DataFrame(rows, columns=headers)
df.iloc[:, 1:] = df.iloc[:, 1:].apply(pd.to_numeric)
print(df)
Earnings estimate Current qtr. (Jun 2024) Next qtr. (Sep 2024)
0 No. of analysts 13.0 11.0
1 Avg. Estimate 1.52 1.62
2 Low estimate 1.36 1.3
3 High estimate 1.61 1.74
4 Year ago EPS 1.76 1.36
Current year (2024) Next year (2025)
0 26.0 26.0
1 6.27 7.23
2 5.02 5.88
3 6.66 8.56
4 5.74 6.27
</code>
<code>import pandas as pd
data = [
['Earnings estimate', 'Current qtr. (Jun 2024)', 'Next qtr. (Sep 2024)', 'Current year (2024)', 'Next year (2025)'],
['No. of analysts', '13', '11', '26', '26'],
['Avg. Estimate', '1.52', '1.62', '6.27', '7.23'],
['Low estimate', '1.36', '1.3', '5.02', '5.88'],
['High estimate', '1.61', '1.74', '6.66', '8.56'],
['Year ago EPS', '1.76', '1.36', '5.74', '6.27']
]
headers = data[0]
rows = data[1:]
df = pd.DataFrame(rows, columns=headers)
df.iloc[:, 1:] = df.iloc[:, 1:].apply(pd.to_numeric)
print(df)
Earnings estimate Current qtr. (Jun 2024) Next qtr. (Sep 2024)
0 No. of analysts 13.0 11.0
1 Avg. Estimate 1.52 1.62
2 Low estimate 1.36 1.3
3 High estimate 1.61 1.74
4 Year ago EPS 1.76 1.36
Current year (2024) Next year (2025)
0 26.0 26.0
1 6.27 7.23
2 5.02 5.88
3 6.66 8.56
4 5.74 6.27
</code>
import pandas as pd
data = [
['Earnings estimate', 'Current qtr. (Jun 2024)', 'Next qtr. (Sep 2024)', 'Current year (2024)', 'Next year (2025)'],
['No. of analysts', '13', '11', '26', '26'],
['Avg. Estimate', '1.52', '1.62', '6.27', '7.23'],
['Low estimate', '1.36', '1.3', '5.02', '5.88'],
['High estimate', '1.61', '1.74', '6.66', '8.56'],
['Year ago EPS', '1.76', '1.36', '5.74', '6.27']
]
headers = data[0]
rows = data[1:]
df = pd.DataFrame(rows, columns=headers)
df.iloc[:, 1:] = df.iloc[:, 1:].apply(pd.to_numeric)
print(df)
Earnings estimate Current qtr. (Jun 2024) Next qtr. (Sep 2024)
0 No. of analysts 13.0 11.0
1 Avg. Estimate 1.52 1.62
2 Low estimate 1.36 1.3
3 High estimate 1.61 1.74
4 Year ago EPS 1.76 1.36
Current year (2024) Next year (2025)
0 26.0 26.0
1 6.27 7.23
2 5.02 5.88
3 6.66 8.56
4 5.74 6.27