Showing posts with label source code. Show all posts
Showing posts with label source code. Show all posts

Sep 28, 2022

[Python] to compare/calculate Pandas columns

to compare Pandas columns, 
    print(df['Column1'].equals(df['Column2'])) 

if df['Column1'] is equal to df['Column2'], it will return "True" otherwise "False" 


to calculate/assign values,
    print(df['Column1'] + df['Column2']) 

import numpy as np
import pandas as pd

a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])

df['que'] = np.where((df['one'] >= df['two']) & (df['one'] <= df['three']), df['one'], np.nan)

result will look like:
          one  two three  que
   0     10    1.2   4.2    10
   1      5     70    0.03  NaN
   2      8     5      0       NaN

Sep 27, 2022

[Python] how to find the first N largest/smallest values

import pandas as pd

temp= pd.read_csv("temp.txt", delimiter="\t", skipinitialspace=True, encoding= "utf-8")
print(temp["score"].nlargest(5))    # print top 5 values
print(temp["score"].smallest(5))    # print bottom 5 values

print(temp.loc[((temp['score'] >80) & (temp['score'] <=90))  |  ((temp['math'] >=80) & (temp['math'] <90))].nlargest(5))    # print conditioned top 5 values  

Mar 19, 2022

[Python] remove all annotations starting with #

import re
input= open('input.py', 'r', encoding='utf-8').read()
# convert a line (\n) starting with ## into an empty line (\n)  
output= re.sub(re.compile(r"##.*[\n]" ) , "\n" , input)    
open('output.py', 'w', encoding='utf-8').write(output)

Aug 15, 2021

[Python] statistics with Pandas DataFrame

import pandas as pd 

ng_list = [{"name": "Jake", "math": 61, "chemistry": 80}, {"name": "Annie", "math": 78, "chemistry": 90}, {"name": "Jane", "math": 71, "chemistry": 54}, {"name": "Sam", "math": 75, "chemistry": 74}, {"name": "Ben", "math": 46, "chemistry": 64}, {"name": "Sky", "math": 38, "chemistry": 77}]  

# create data frame 
df = pd.DataFrame(ng_list) 
print(df, "\n") 

# sum for each row
df["total"] = df["math"] + df["chemistry"]
print(df, "\n") 

# sum for each column
print(df["math"].sum(), "\n") 

# mean with condition
mean_math_bet60_80 = df.loc[(df["math"] >= 60) & (df["math"] <= 80), "math"].mean()
print(mean_math_bet60_80, "\n")

# common statistics for numerical values
print(df.describe(), "\n")

# common statistics for non-numerical values
print(df.describe(include='object'), "\n")


# functions for other staticstics:
# max, min: maximum, mimimum values 
# count: count
# sem: standard error
# mode: most frequently appeared value
# quantile: quantile, e.g., 10% quantile --> df['math'].quantile(0.1)
# corr: corelation between two columes, e.g., df['math'].corr(df['chemistry'])

[Python] concatenate two data frame, and deal with NaN

import pandas as pd 

ng_list1 = [{"name": "Jake", "grade": 80}, {"name": "Annie", "grade": 90}, {"name": "Jane"}] 
ng_list2 = [{"name": "Sam", "grade": 74}, {"name": "Ben", "grade": 64}, {"name": "Sky", "grade": 77}, {"name": "Annie", "grade": 90}] 
 
# create data frame 
df1 = pd.DataFrame(ng_list1)
df2 = pd.DataFrame(ng_list2)
print(df1, "\n")
print(df2, "\n")

# concatenate df1 and df2
df = pd.concat([df1, df2], ignore_index=True)
print(df, "\n") 

# remove duplicates
df.drop_duplicates(inplace=True, ignore_index=True) 

# dealing with NaN
# if you wanna drop any row including NaN, use df.dropna(inplace=True)
# if you wanna drop a row with NaN for all columns, use df.dropna(how="all", inplace=True)
# if you wanna fill all NaN with 0, use df.fillna(0, inplace=True)
df.fillna(0, inplace=True)
print(df, "\n") 

[Python] sort data frame and reset index

import pandas as pd
import numpy as np


n = ["Jake", "Annie", "Charles", "Sam", "David"]
g = [80, 90, 75, 64, 78]

# create a data frame 
df = pd.DataFrame()
df["name"], df["grade"] = n, g 
print(df, "\n")

# sort by name
df.sort_values(by=["name"], ascending=[True], inplace=True)  
print(df, "\n")

# reset index
df.reset_index(inplace=True, drop=True)
print(df, "\n")

[Python] create a new Pandas DataFrame, and add a new column to the data frame

import pandas as pd
import numpy as np


n = ["Annie", "Charles", "David", "Sam"]
g = [80, 90, 75, 78]

# create a data frame using one of lists
df = pd.DataFrame(n, columns=["name"])
print(df, "\n")

# add another list to the data frame
df.insert(len(df.columns), "grade", g)
print(df, "\n")

# dertermine whether pass or not and create a new column for it
df["pass"] = np.where(df["grade"] >=80, "yes", "no")
print(df, "\n")




Aug 9, 2021

[Python] to reset index for a sorted list

import pandas as pd
import numpy as np


data1 = [1, 3, 2, 4]
data2 = [1015, 1014, 1014, 1019]
data3 = [33, 39, 91, 14]

data = [data1, data2, data3]
print('data: ', data, "\n")

data_transposed = np.transpose(data)
print('transposed data: \n', data_transposed, "\n")

df = pd.DataFrame(data_transposed, columns=['quantity', 'date', 'price'])
print('DataFrame: \n', df, "\n")

size = len(df['date'])
evaluation = [df['quantity']*df['price']] 
temp = np.array(evaluation).reshape(size,1) 
df = np.hstack([df, temp]) 
df = pd.DataFrame(df, columns=['quantity', 'date', 'price', 'evaluation'])
print("DataFrame with added column: \n", df, "\n")

df = df.sort_values(['date', 'evaluation'], ascending=[True, True])
df = df[["date", "evaluation", "price", "quantity"]]
print('sorted DataFrame: \n', df, "\n")

df = df.reset_index(drop=True)
print('re-indexed data: \n', df)

[Python] to append/insert data into a list/array

import pandas as pd

data = [[-4, -3, -2, -1], [1, 2, 3, 4]]
data2 = [5, 6, 7, 8]
data3 = [9, 10, 11, 12]

data_list = []
data_list.append(data)
print(data_list)

data_list.append(data2)
print(data_list)

data_list.insert(0, data3)
print(data_list)
print()

# add header (title row) using pandas
df = pd.DataFrame(data, columns=['A', 'B', 'C', 'D'])
print(df, "\n")

temp_df = pd.DataFrame([data2], columns=['A', 'B', 'C', 'D'])
df = pd.concat([df, temp_df]).reset_index(drop=True)
print(df, "\n")

temp_df = pd.DataFrame({"A": data3[0], "B": data3[1], 'C': data3[2], 'D': data3[3]}, index=[0])
df = pd.concat([temp_df, df]).reset_index(drop=True)
print(df, "\n")

# add a new column using pandas
size = len(df)
data2 = [i * 2 for i in range(size)]
temp = pd.Series(data2, name='XYZ')
df = df.join(temp)
print(df)

    

[Python] to sort a list with multiple data types

my_list = [0, "g", "a", "b", "f", 'c', 3, 2, "z"]
print("original list:", my_list)

# sort - method 1
my_list_int = sorted([i for i in my_list if type(i) is int])
my_list_str = sorted([i for i in my_list if type(i) is str])
print("sorted list:", my_list_int + my_list_str)

# sort - method 2
my_list_int = sorted([x for x in my_list if isinstance(x, int)])
my_list_str = sorted([x for x in my_list if isinstance(x, str)])
print("int:", my_list_int)
print("str:", my_list_str)

Aug 7, 2021

[Python] to process time

import time

print(dir(time))    # print components of "time" module
print()

print(time.time(), "\n")    # print time as numeric value calculated from 1970, 1, 1, 00:00:00

t = time.ctime()    # Sat Aug  7 01:19:22 2021
print(t)
time.sleep(1)    # pause for 1 seconds
print(t.split(' ')[-1])    # print 'year' only
print(t.split(' ')[-2].split(':')[-3])    # print "hour" only

now = time.localtime()
filename = "log_%02d%02d%02d_%02d-%02d-%02d.txt" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
print("\n", filename)

Feb 15, 2020

[C/Cpp] an example of permutation

// Permutation,  total number of cases = R^N

#include <stdio.h>

#define N 3
#define R 4 

int temp[R];  
FILE *fp ;


void permutation(int value)
{
  int i;
  
  if(R==value){ 
    for(i=0; i<R; i++){
      printf("%d ", temp[i]);
      fprintf(fp, "%d ", temp[i]);
    }
    printf("\n"); 
    fprintf(fp, "\n");
    return;
  }
  for(i=1; i<=N; i++){
    temp[value]=i;
    permutation(value+1);
  }
}


int main()
{
    fp = fopen("all_cases.txt", "w");
    permutation(0);  
    return 0;
    fclose(fp);
}

[C/Cpp] file I/O

#include <stdio.h>

int main(){

    FILE *fp;
    int i, j;

    fp = fopen("test.txt", "w");
    for(i=0; i<10; i++){
        fprintf(fp, "%d\n", i);
    }
    fclose(fp);


    fp = fopen("test.txt", "r");
    while(fscanf(fp, "%d", &j) != EOF){
        printf("%d\n", j);
    }
    fclose(fp);

    return 0; 
}

 

Jan 1, 2018

[Python] an example of I/O, file I/O

a = float(input('enter any number: '))
print('%f * %f = %f\n' % (a, a, a*a))


a,b = input('enter any two numbers: ').split()  # split(): two numbers with space in between;  split(','): two numbers with , in between
print('%f * %f = %f\n' % (float(a), float(b), float(a)*float(b)))


now = time.localtime()
filename = "log_%02d%02d%02d_%02d:%02d:%02d.txt" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
f = open(filename, 'w')  # r: read-only,  r+: read and write, w: write,  a: append
f.write('abcdefgh')
f.close()


g = open('t.txt', 'r')
line = g.readline()
while line:
 print (line, end='\n')
 line = g.readline()
g.close()


h = open('t.txt', 'r')
lines = h.readlines()
for line in lines:
 print(line*2, end=' ')
h.close()


Result------------------------------------------
enter any number: 1.000000 * 1.000000 = 1.000000

enter any two numbers: 1.000000 * 2.000000 = 2.000000

abcdefgh
abcdefghabcdefgh 

[Python] an example of function, module

def add3(a,b,c):  # define a function
 return(a+b+c)
print(add3(1,2,3),"\n")
 
 
def add(*vary):
 for i in vary:
  print(i)
add(1,2,3,4)

 

# if the above function is saved in add.py and you'd like to use it as a module,
#  import add
#  add.add3(1,3,4)
#
# if you'd like to directly use all functions in add.py,
#  from add import *
#  add3(2,4,5)

[Python] an example of if, while, for

a=500
if (500<a<600):
 print("a>500")
elif (a==500) or (a==400):
 print("a=500")
else:
 print("a<500")
 

while a>450:
 print(a, a-1, a-2, a-3, a-4)
 a=a-5
print()
 

while a>400:
 print(a, a-1, a-2, a-3, a-4)
 a=a-5
 if a==415: break
print()
 
 
while a>300:
 a=a-5
 if a%10==0: continue
 print(a, a-1, a-2, a-3, a-4)
print()


x=[1,2,3,4,5]
for i in x:
 print(i/2)
print()
 

for i in range(5):
 for j in range(2,5):
  print(i,j)
print()
 
for i in range(len(x)):
 print(i)
print()

Result---------------------------------------------
a=500
500 499 498 497 496
495 494 493 492 491
490 489 488 487 486
485 484 483 482 481
480 479 478 477 476
475 474 473 472 471
470 469 468 467 466
465 464 463 462 461
460 459 458 457 456
455 454 453 452 451

450 449 448 447 446
445 444 443 442 441
440 439 438 437 436
435 434 433 432 431
430 429 428 427 426
425 424 423 422 421
420 419 418 417 416

405 404 403 402 401
395 394 393 392 391
385 384 383 382 381
375 374 373 372 371
365 364 363 362 361
355 354 353 352 351
345 344 343 342 341
335 334 333 332 331
325 324 323 322 321
315 314 313 312 311
305 304 303 302 301

0.5
1.0
1.5
2.0
2.5

0 2
0 3
0 4
1 2
1 3
1 4
2 2
2 3
2 4
3 2
3 3
3 4
4 2
4 3
4 4

0
1
2
3
4

[Python] an example of print-formatting, list-handling

a=3.141592
b=1.23
c=89
print(format(a, '.2f'), format(a, '10.2f'))
print('{0:5f}'.format(a), '{0:5f}'.format(b), '{0:5d}'.format(c))
print('{0:>8f}'.format(a), '{0:>8f}'.format(b), '{0:>8d}'.format(c))
print('{0:<8f}'.format(a), '{0:<8f}'.format(b), '{0:<8d}'.format(c))
print()

d="AbCdbeFgbhcC"
print(d[1:3], d[0], d[1:], d[:5])
print(d.upper(), d.lower())
print(d[1:3].upper(), d[:5].lower())
print(d.find('de'))
print(d.find('de',3))
print(d.find('de',4))
print(d.find('the'))
print(d.count('c'))
print(d.replace('C','Z'))
print(d.split('b'))

e=d.split('b')
f="="
print(f.join(e))
print()

e.append('ddddd')
e.append(3)
print(e)
e.insert(2,'xx')
print(e)
e.extend([1,1,2,'vv'])
print(e)
print(e.index(1), e.index('xx'))
print(e.count(1))
e.pop(0)
print(e)
e.pop()
print(e)
e.remove('xx')
print(e)
print()

g=[1,3,4,2,0]
g.sort()
print(g)
g.sort(reverse=True)
print(g)

h=['x','aa','b','yy','c','d','e']
h.sort()
print(h)
h.reverse()
print(h)


Result--------------------------------------------------------
3.14       3.14
3.141592 1.230000    89
3.141592 1.230000       89
3.141592 1.230000 89      

bC A bCdbeFgbhcC AbCdb
ABCDBEFGBHCC abcdbefgbhcc
BC abcdb
-1
-1
-1
-1
1
AbZdbeFgbhcZ
['A', 'Cd', 'eFg', 'hcC']
A=Cd=eFg=hcC

['A', 'Cd', 'eFg', 'hcC', 'ddddd', 3]
['A', 'Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3]
['A', 'Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2, 'vv']
7 2
2
['Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2, 'vv']
['Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2]
['Cd', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2]

[0, 1, 2, 3, 4]
[4, 3, 2, 1, 0]
['aa', 'b', 'c', 'd', 'e', 'x', 'yy']
['yy', 'x', 'e', 'd', 'c', 'b', 'aa']