Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract distinct values from Dataframe and insert them into new Dataframe with same column Name

Using python 3.7 , pandas 1.1.3 , Anaconda Jupyter Notebook

I am new into python and I have a following dataframe.

DF_1

Name  Date
AAA   2000-09-01
BBB   2001-08-01
CCC   2002-07-01
AAA   2005-05-01

I just want to extract distinct values from 'Name' column and create a new dataframe (df_2) and insert into it with same column name.

Output df_2 should look like this

Name
AAA
BBB
CCC
like image 298
Zaw Lynn Htut Avatar asked Jan 27 '21 06:01

Zaw Lynn Htut


People also ask

How do I extract unique values from a column in pandas?

You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.

How do you extract unique values in Python?

Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.

How do you get only unique rows in pandas?

And you can use the following syntax to select unique rows across specific columns in a pandas DataFrame: df = df. drop_duplicates(subset=['col1', 'col2', ...])

How to create a copy of a Dataframe with specific columns?

Alternatively, You can also use DataFrame.filter () method to create a copy and create a new DataFrame by selecting specific columns. Yields output same as above. 3. Using DataFrame.transpose () Method DataFrame.transpose () method is used to transpose index and column. It reflects the DataFrame writing rows as columns and vice-versa.

How to replace values from another Dataframe when different indices are used?

So to replace values from another DataFrame when different indices we can use: Now the values are correctly set: You can use Pandas merge function in order to get values and columns from another DataFrame. For this purpose you will need to have reference column between both DataFrames or use the index.

How to extract a column from a Dataframe in Python?

The original dataframe looks like this import pandas as pd df = pd.DataFrame(data = temp['data']['weather']) df.head() The first one is simple, it takes a dataframe and the name of a column, and it will extract the column into a new dataframe.

How to create new Pandas Dataframe by selecting specific columns?

You can create new pandas DataFrame by selecting specific columns by using DataFrame.copy (), DataFrame.filter (), DataFrame.transpose (), DataFrame.assign () functions. DataFrame.iloc [] and DataFrame.loc [] are also used to select columns.


1 Answers

Try this:

df_2 = DF_1[['Name']].drop_duplicates()
like image 116
Gleb V Avatar answered Sep 27 '22 18:09

Gleb V