Snippet 1 import sys import csv import glob import pandas as pd # get data file names path =r’C:DRODCL_rawdata_filesexcelfiles’ filenames = glob.glob(path + “/*.xlsx”) dfs = [] for df in dfs: xl_file = pd.ExcelFile(filenames) df=xl_file.parse(‘Sheet1’) dfs.concat(df, ignore_index=True) Snippet 2 import os import pandas as pd import openpyxl as excel import glob #setting up path path… Continue reading Pandas Reading Each Xlsx File In Folder Code Example
Tag: Pandas
Read All Files And Store In One Dataframe Pandas Code Example
Snippet 1 path = r’C:DRODCL_rawdata_files’ # use your path all_files = glob.glob(os.path.join(path, “*.csv”)) # advisable to use os.path.join as this makes concatenation OS independent df_from_each_file = (pd.read_csv(f) for f in all_files) concatenated_df = pd.concat(df_from_each_file, ignore_index=True) Copyright © Code Fetcher 2020
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
Pandas Groupby Count As New Column Code Example
Snippet 1 In [12]: df.groupby([“item”, “color”])[“id”].count().reset_index(name=”count”) Out[12]: item color count 0 car black 2 1 truck blue 1 2 truck red 2 Copyright © Code Fetcher 2020
Pandas Groupby Mean Code Example
Snippet 1 df.groupby([‘A’, ‘B’]).mean() Snippet 2 In [57]: df.groupby([‘cluster’, ‘org’]).mean() Out[57]: time cluster org 1 a 438886 c 23 2 d 9874 h 34 3 w 6 Copyright © Code Fetcher 2020
Pandas Groupby Mean Round Code Example
Snippet 1 data.mean().round(0) # Rounds mean to nearest integer, e.g. 1.95 = 2 and 1.05 = 1 Copyright © Code Fetcher 2020
Pandas Iterrows Code Example
Snippet 1 >>> df = pd.DataFrame([[1, 1.5]], columns=[‘int’, ‘float’]) >>> row = next(df.iterrows())[1] >>> row int 1.0 float 1.5 Name: 0, dtype: float64 >>> print(row[‘int’].dtype) float64 >>> print(df[‘int’].dtype) int64 Snippet 2 for index, row in flights.head(n=2).iterrows(): print(index, row) Snippet 3 for index, row in df.iterrows(): print(row[‘c1’], row[‘c2’]) Snippet 4 for index, row in df.iterrows(): print(row[‘c1’],… Continue reading Pandas Iterrows Code Example
Pandas Correlation Function Code Example
Snippet 1 # importing pandas as pd import pandas as pd # Making data frame from the csv file df = pd.read_csv(“nba.csv”) # To find the correlation among # the columns using kendall method df.corr(method =’kendall’) Snippet 2 # To find the correlation among # the columns using pearson method df.corr(method =’pearson’) Copyright… Continue reading Pandas Correlation Function Code Example