๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ „์ฒด ๊ธ€36

[python/ํŒŒ์ด์ฌ] ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ - loc์™€ iloc๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์ถ”์ถœ loc์™€ iloc์˜ ์ฐจ์ด loc ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์˜ ํ–‰์ด๋‚˜ ์ปฌ๋Ÿผ์— label์ด๋‚˜ boolean array๋กœ ์ ‘๊ทผ location์˜ ์•ฝ์–ด๋กœ, ์ธ๊ฐ„์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” label ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ iloc ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์˜ ํ–‰์ด๋‚˜ ์ปฌ๋Ÿผ์— ์ธ๋ฑ์Šค ๊ฐ’์œผ๋กœ ์ ‘๊ทผ integer location์˜ ์•ฝ์–ด๋กœ, ์ปดํ“จํ„ฐ๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” indexing ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ import pandas as pd df = pd.DataFrame({'a' : [i for i in range(1, 11)], 'b' : [i for i in range(11, 21)], 'c' : [i for i in range(21, 31)]}) df -๋ฌธ์ œ : a, b์—ด์„ ์ถ”์ถœํ•˜๊ธฐ df['a', 'b'] df[['a', 'b']] ์ฃผ์˜ํ•ด์•ผํ•  ์ .. 2022. 7. 23.
[python/ํŒŒ์ด์ฌ] ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ - ์‹œ๋ฆฌ์ฆˆ(Series) ์‹œ๋ฆฌ์ฆˆ(Series) ๋ฐ์ดํ„ฐ ์ƒ์„ฑ import pandas as pd df = pd.DataFrame({'a' : [1, 2, 3], 'b' : [4, 5, 6], 'c' : [7, 8, 9]}) df -a ์—ด ์ถ”์ถœํ•˜๊ธฐ df['a'] 0 1 1 2 2 3 Name: a, dtype: int64 type(df['a']) pandas.core.series.Series ์‹œ๋ฆฌ์ฆˆ ์ƒ์„ฑ๋ฐฉ๋ฒ• a = pd.Series(data, index=index, name=name) # ํ–‰์˜ ์ด๋ฆ„(index), ์—ด์˜ ์ด๋ฆ„(name). ๋”ฐ๋กœ ์ž…๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด index๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ •์ˆ˜๊ฐ’, name์€ ๋นˆ ๊ฐ’(None)์ด ์ž…๋ ฅ๋จ ์˜ˆ) a = pd.Series([1, 2, 3, 1, 2, 3]) a 0 1 1 2 2 3 3 1 4.. 2022. 7. 22.
๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ - copy๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ 3) copy๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ ์›๋ณธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค ๋ณต์‚ฌ์— ๋Œ€ํ•ด ์ œ๋Œ€๋กœ ์•Œ์ง€ ๋ชปํ•œ ์ฑ„๋กœ ํ•จ๋ถ€๋กœ ๋ณต์‚ฌํ•˜๋ฉด, ์›๋ณธ ๊ฐ์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค ๋ฐ์ดํ„ฐ ์ƒ์„ฑ import pandas as pd df = pd.DataFrame({'a': [1, 2, 3], 'b' : [4, 5, 6], 'c' : [7, 8, 9]}) -๋ฌธ์ œ : ํ•„๋“œ๋ช…์„ a, b, c ์—์„œ d, e, f๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ df.columns = ['d', 'e', 'f'] df -๋ฌธ์ œ : ํ•„๋“œ๋ช… a๋ฅผ '์—์ด'๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ df df = pd.DataFrame({'a': [1, 2, 3], 'b' : [4, 5, 6], 'c' : [7, 8, 9]}) df.rename(columns = {'a' :.. 2022. 7. 22.
[python/ํŒŒ์ด์ฌ] ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ - ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ƒ์„ฑ, ์นผ๋Ÿผ๋ช… ์ถ”์ถœ/๋ณ€๊ฒฝ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ 1) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ƒ์„ฑ dict ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ƒ์„ฑ import pandas as pd df = pd.DataFrame({'a' : [1, 2, 3], 'b' : [4, 5, 6], 'c' : [7, 8, 9]}) type(df) pandas.core.frame.DataFrame df dummy = {'a': [1, 2, 3], 'b' : [4, 5, 6], 'c' : [7, 8, 9]} df2 = pd.DataFrame(dummy) df2 List ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ƒ์„ฑ a = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] df3 = pd.DataFrame(a) df3 df3.columns = ['a', 'b', 'c'] df3 ๋ฌธ์ œ : ์•„๋ž˜ ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™.. 2022. 7. 22.