728x90

ํŒŒ์ด์ฌ 20

[Python] ํŒ๋‹ค์Šค concat, append, join, merge ์ฐจ์ด

Pandas concat vs append vs join vs merge Concat gives the flexibility to join based on the axis( all rows or all columns) Append is the specific case(axis=0, join='outer') of concat Join is based on the indexes (set by set_index) on how variable =['left','right','inner','couter'] Merge is based on any particular column each of the two dataframes, this columns are variables on like 'left_on', 'ri..

[Python] Pandas Dataframe ์ค‘๋ณต ์ œ๊ฑฐ, distinctํ•œ ๊ฐ’ ํ™•์ธ

df.drop_duplicates() df ์ „์ฒด์˜ ์ค‘๋ณต ์ œ๊ฑฐ๋„ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์—ด ๋ผ๋ฆฌ ์ค‘๋ณต ์ œ๊ฑฐ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์œ„์˜ ๋ฐ์ดํ„ฐ๋Š” pert_iname์ด๋ผ๋Š” ์—ด์— ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋“ค์ด ๋งŽ์ด ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ df.drop_duplicates()๋กœ distinctํ•œ ๊ฐ’์€ ๋ช‡ ๊ฐœ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์›๋ž˜ 13553๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘๋ณต๊ฐ’์„ ์ œ์™ธํ•˜๋ฉด 6798๊ฐœ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ df.value_counts() ๋ฅผ ์ด์šฉํ•˜๋ฉด distinctํ•œ ๊ฐ’์„ ์ฐพ์•„์ฃผ๋ฉด์„œ ๋ช‡ ๊ฐœ๊ฐ€ ์ค‘๋ณต๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

[Python] sys.path ๋ชจ๋“ˆ ์‚ฌ์šฉ, ์ƒ๋Œ€๊ฒฝ๋กœ

sys ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ์ƒ๋Œ€๊ฒฝ๋กœ ์„ค์ • ๊ฐ€๋Šฅ import sys sys.path.append('๋‚ด๊ฒฝ๋กœ') ์œ„ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ๋‚ด ๊ฒฝ๋กœ๊ฐ€ ํŒŒ์ผ ์‹คํ–‰ ์œ„์น˜๊ฐ€ ๋˜๊ณ  ๋‹ค๋ฅธ ํŒŒ์ผ์„ import ํ•  ๋•Œ from ~ import ~๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ex) parent ํด๋”์— child ํด๋”๊ฐ€ ์กด์žฌํ•˜๊ณ , child ํด๋” ์•ˆ์— myfuncํ•จ์ˆ˜๋ฅผ ๋‹ด์€ example.py ์žˆ๋‹ค๋ฉด import sys sys.path.append('C:/Parent') from child.example import myfunc ์œ„์ฒ˜๋Ÿผ myfuncํ•จ์ˆ˜๋ฅผ ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋‹ค ์จ์ฃผ์ง€ ์•Š๊ณ ๋„ ํŽธํ•˜๊ฒŒ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ํŒŒ์ผ ์œ„์น˜๊ฐ€ ๋ฐ”๋€๋‹ค๊ฑฐ๋‚˜ ํ•˜๋ฉด ๋ถˆํŽธํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค.

[Python] Pandas dataframe ๊ฒฐํ•ฉ, ์กฐ์ธ, ๋ณ‘ํ•ฉ(Join, Merge)

Join 1. ์˜ˆ์‹œ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ import pandas as pd df = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'], 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']}) other = pd.DataFrame({'key': ['K0', 'K1', 'K2'], 'B': ['B0', 'B1', 'B2']}) 2. ์—ด์˜ Index ์ง€์ •ํ•ด์„œ Join df.join(other, lsuffix = '_caller', rsuffix = '_other') 3. Key๋ฅผ index๋กœ ์ง€์ •ํ•ด Join df.set_index('key').join(other.set_index('key')) 4. join ๋ฉ”์†Œ๋“œ์˜ parameter ..

[์›นํฌ๋กค๋ง] Python์œผ๋กœ ์›น ํฌ๋กค๋ง, HTML ํŒŒ์‹ฑ, requests, BeautifulSoup ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉ

์›น ํฌ๋กค๋ง์œผ๋กœ href ํƒœ๊ทธ์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ (HTML ํŒŒ์‹ฑ) import requests from bs4 import BeautifulSoup ftp.ensembl.org/pub/current_regulation/homo_sapiens/RegulatoryFeatureActivity/ Index of /pub/current_regulation/homo_sapiens/RegulatoryFeatureActivity/ ftp.ensembl.org ์ด๋ ‡๊ฒŒ ์ƒ๊ธด ์›น ํ˜•ํƒœ์—์„œ ๋ฐ์ดํ„ฐ ์ด๋ฆ„๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ์›น ํฌ๋กค๋ง ์‹ค์Šต. 1. requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด url get ํ•˜๊ธฐ import requests from bs4 import BeautifulSoup res = requests.get('http://ftp.en..

[Python] Pandas Dataframe ์—ด์— ์–ด๋–ค ๋ฐ์ดํ„ฐ ์žˆ๋Š”์ง€ value ํ™•์ธ, ๋ฐ์ดํ„ฐ ๋ณ„๋กœ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ, ์ค‘๋ณต๊ฐ’ ํ™•์ธ, ์œ ์ผํ•œ(์œ ๋‹ˆํฌํ•œ) ๊ฐ’ ์ฐพ๊ธฐ

df.unique() ์œ„์˜ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์€ cell_line์— ๋Œ€ํ•œ ๋‚ด ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ์ด๋‹ค. ์ด์ œ ์ด cell_lien ๋ฐ์ดํ„ฐ์—์„œ ์œ ๋‹ˆํฌํ•œ ๊ฐ’์„ ์ฐพ์•„๋ณผ ๊ฒƒ์ด๋‹ค. 1. ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๊ฐ’ ํ™•์ธ cell_line = f['epigenomes_with_experimental_evidence'].values # values = df[์ปฌ๋Ÿผ๋ช…].values ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด arrayํ˜•ํƒœ๋กœ ๋ชจ๋“  ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค. 2. ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์—์„œ ๊ฐ ์š”์†Œ๋ณ„๋กœ ๋ช‡๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ f['epigenomes_with_experimental_evidence'].value_counts() # df[์ปฌ๋Ÿผ๋ช…].value_counts() 3. ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  ์œ ๋‹ˆํฌํ•œ ๊ฐ’ ํ™•์ธ f['epigenome..

728x90