Ask Your Question
0

What is the method to form a dataframe using two lists?

asked 2021-08-03 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-26 16:00:00 +0000

huitzilopochtli gravatar image

One possible method to form a dataframe using two lists is to use the pandas library in Python. Here's an example code:

import pandas as pd

# Create two lists of data
list1 = [1, 2, 3]
list2 = ['A', 'B', 'C']

# Combine the lists into a dictionary
data = {'col1': list1, 'col2': list2}

# Create the dataframe using the dictionary
df = pd.DataFrame(data)

# Display the dataframe
print(df)

Output:

   col1 col2
0     1    A
1     2    B
2     3    C

In this example, the two lists list1 and list2 are combined into a dictionary called data, where the keys are the column names and the values are the data for each column. Then, the dataframe df is created using the pd.DataFrame() function, passing the data dictionary as argument. Finally, the dataframe is displayed using the print() function.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-03 11:00:00 +0000

Seen: 11 times

Last updated: Aug 26 '21