Ask Your Question
1

How can a single-column be made from a pandas data frame that has n columns?

asked 2022-08-21 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-07-13 15:00:00 +0000

scrum gravatar image

A single-column can be made from a pandas data frame that has n columns by selecting one of the n columns and creating a new data frame with only that column. Here is an example code:

import pandas as pd

# create a data frame with three columns
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]})

# select a single column and create a new data frame
single_col_df = df['a']

# print the new data frame
print(single_col_df)

Output:

0    1
1    2
2    3
Name: a, dtype: int64

In this example code, the df data frame has three columns 'a', 'b', and 'c'. The 'a' column is selected by using df['a'] and stored as a new data frame single_col_df. The output shows that the new data frame has only one column 'a' with the same rows and values as the original 'a' column in the df data frame.

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: 2022-08-21 11:00:00 +0000

Seen: 15 times

Last updated: Jul 13 '22