Pandas Pivot To Sparse Code Example

Snippet 1 from scipy.sparse import csr_matrix from pandas.api.types import CategoricalDtype person_c = CategoricalDtype(sorted(frame.person.unique()), ordered=True) thing_c = CategoricalDtype(sorted(frame.thing.unique()), ordered=True) row = frame.person.astype(person_c).cat.codes col = frame.thing.astype(thing_c).cat.codes sparse_matrix = csr_matrix((frame[“count”], (row, col)), shape=(person_c.categories.size, thing_c.categories.size)) >>> sparse_matrix >>> sparse_matrix.todense() matrix([[0, 1, 0, 1], [1, 0, 0, 1], [1, 0, 1, 0]], dtype=int64) dfs = pd.SparseDataFrame(sparse_matrix, index=person_c.categories, columns=thing_c.categories, default_fill_value=0) >>>… Continue reading Pandas Pivot To Sparse Code Example

Pandas Dataframe Drop Examples

movie_award_2022.drop([‘title’,’movie_id’], axis=1, inplace=True) # drop duplicate columns source cs3 = cs2.drop([‘Gender’,’Education’,’Occupation’,’Region’,’Rented_OwnHouse’],axis = 1) cs3.drop([‘Good_Bad’],axis = 1, inplace = True) cs3 source #Drop missing data df2.dropna() source # drop missing vallues gobike_df = gobike_df.dropna() source Copyright © Code Fetcher 2022    

Pandas Dataframe Froms String Code Example

Snippet 1 import sys if sys.version_info[0] < 3: from StringIO import StringIO else: from io import StringIO import pandas as pd TESTDATA = StringIO("""col1;col2;col3 1;4.4;99 2;4.5;200 3;4.7;65 4;3.2;140 """) df = pd.read_csv(TESTDATA, sep=";") Similar Snippets Pandas Dataframe Froms String Code Example - pandas Combine Two Dataframe In Pandas Code Example - pandas Pandas Frame Convert… Continue reading Pandas Dataframe Froms String Code Example

Pandas Drop Rows With Condition Code Example

Snippet 1 # import pandas library import pandas as pd    # dictionary with list object in values details = {     ‘Name’ : [‘Ankit’, ‘Aishwarya’, ‘Shaurya’,               ‘Shivangi’, ‘Priya’, ‘Swapnil’],     ‘Age’ : [23, 21, 22, 21, 24, 25],     ‘University’ : [‘BHU’, ‘JNU’, ‘DU’, ‘BHU’,                      ‘Geu’, ‘Geu’], }    # creating a Dataframe object  df = pd.DataFrame(details,… Continue reading Pandas Drop Rows With Condition Code Example