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
Tag: Pandas
Pandas Cumulative Sum Column Code Example
Snippet 1 import pandas as pd from random import randint df = pd.DataFrame(data=[{‘duration’: randint(0,3)} for _ in range(5)]) df.head() # duration # 0 0 # 1 2 # 2 1 # 3 0 # 4 3 df[‘cum_dur’] = df.duration.cumsum() df.head() # duration cum_dur # 0 0 0 # 1 2 2 # 2 1 3… Continue reading Pandas Cumulative Sum Column Code Example
Pandas Print Index Code Example
Snippet 1 # Import pandas package import pandas as pd # making data frame data = pd.read_csv(“data.csv”) print(data.index) Copyright © Code Fetcher 2020
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 Dataframe To Change Data To Horizontally Code Example
Snippet 1 | id | name | country01 | sort01 | country02 | sort02 | country03 | sort03 | | 1 | Foo | USA | 1 | Japan | 2 | China | 3 | | 2 | Bar | USA | 1| UK | 3 | France | 4 | | 3 |… Continue reading Pandas Dataframe To Change Data To Horizontally Code Example
Pandas Dataframe To Json
Copyright © Code Fetcher 2022 Code Fetcher Python Pandas Vlsi Notebooks
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
Pandas Fill Na With Value From Another Column Code Example
Snippet 1 df[‘Cat1’].fillna(df[‘Cat2’]) Copyright © Code Fetcher 2020
Pandas Find Fifth Caracter In Field And Change Cell Based On That Number Code Example
Snippet 1 df[‘col1’] = np.where(df[‘col1’] == 0, df[‘col2’], df[‘col1’]) df[‘col1’] = np.where(df[‘col1’] == 0, df[‘col3’], df[‘col1’]) Snippet 2 data[‘result’] = data[‘result’].map(lambda x: x.lstrip(‘+-‘).rstrip(‘aAbBcC’)) Copyright © Code Fetcher 2020
Pandas Frame Convert String Code Example
Snippet 1 df[‘DataFrame Column’] = pd.to_numeric(df[‘DataFrame Column’]) Copyright © Code Fetcher 2020