728x90

์ฝ”๋”ฉ 20

[Python] ํŒ๋‹ค์Šค ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„(Pandas DataFrame) sys:1: DtypeWarning: Columns have mixed types.Specify dtype option on import or set low_memory=False. ๋ฌด์‹œํ•˜๊ธฐ

column์— NaN๊ฐ’์ด๋‚˜ ์—ฌ๋Ÿฌ type์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์„ž์—ฌ ์žˆ์œผ๋ฉด ์ด์™€ ๊ฐ™์€ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด ๋•Œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์•Œ๋ ค์ฃผ๋Š” ๋Œ€๋กœ dtype option์œผ๋กœ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ๊ฑฐ๋‚˜ low_memory = False๋กœ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. pd.read_csv('[ํŒŒ์ผ๋ช…].txt', delimiter = '\t', low_memory=False)

[Python] ํŒ๋‹ค์Šค ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„(pandas dataframe) SettingWithCopyWarning ํ•ด๊ฒฐ

๊ธฐ์กด ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ผ๋ถ€๋ฅผ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜ ์ธ๋ฑ์‹ฑ ํ›„ ๊ฐ’์„ ์ˆ˜์ •ํ•  ๋•Œ ์ข…์ข… ๋ฐœ์ƒํ•œ๋‹ค. ๊ธฐ์กด ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๊ฐ€์ ธ์™€(๋ณต์‚ฌ) ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๋งŒ๋“ค ๋•Œ ์›๋ณธ์„ ์ˆ˜์ •ํ•  ์ง€ ๋ณต์‚ฌ๋ณธ์„ ์ˆ˜์ •ํ•  ์ง€ ๋ชฐ๋ผ์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ผ๊ณ  ํ•œ๋‹ค. ๋‘ ๊ฐ€์ง€ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜๋Š” ๊ฒฝ๊ณ ๋ฅผ ๋ฌด์‹œํ•˜๋Š” ๊ฒƒ์ด๊ณ  ํ•˜๋‚˜๋Š” copy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ! 1. ๊ฒฝ๊ณ  ๋ฌด์‹œ pd.set_option์„ ์‚ฌ์šฉํ•œ ๊ฒฝ๊ณ ๋ฌธ ์ œ๊ฑฐ # SettingWithCopyError --> ์˜ค๋ฅ˜ raise ๋กœ ์ฝ”๋“œ ์‹คํ–‰ X pd.set_option('mode.chained_assignment', 'raise') # SettingWithCopyWarning --> ์‹คํ–‰์€ ๋˜์ง€๋งŒ ๊ฒฝ๊ณ ๋ฌธ ๋œธ pd.set_option('mode.chained_assignment', 'warn') # err..

[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์œผ๋กœ ์›น ํฌ๋กค๋ง, 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