Created
February 23, 2021 20:20
-
-
Save s1rat-dev/53a5ede929c060618d14e14390b104eb to your computer and use it in GitHub Desktop.
[PYTHON] Pandas, Dataframe olayı ile ilgili, kolon ve satırların seçilmesi, ve her hangi bir kolonun silinme işlemi.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from numpy.random import randn | |
df = pd.DataFrame(randn(3,3), index=['A','B','C'], columns=['Column1','Column2','Column3']) | |
result = df | |
################# | |
# SELECT COLUMN # | |
################# | |
result = df['Column1'] # df.loc[':','Column1'] ile aynı kullanım. | |
result = type(df['Column1']) | |
# <class 'pandas.core.series.Series'> | |
result = df[['Column1','Column2']] | |
################## | |
# SELECT ROW # | |
################## | |
''' Row seçerken loc tercih edilir ve 'loc'u fonksiyon gibi '()' ile değil de index arıyor gibi '[]' ile birlikte kullanılır. ''' | |
# loc['row','column'] => loc['row'] => loc[':','Column'] | |
result = df.loc['A'] | |
result = type(df.loc['A']) | |
result = df.loc[:,['Column1','Column2']] | |
result = df.loc[:,'Column1':'Column3'] # Başlangıç ve bitişler de dahildir. | |
result = df.loc[:,:'Column3'] | |
result = df.loc['A':'C',:'Column2'] | |
result = df.loc['B':'C',:'Column1'] | |
######################## | |
# LOC WITH INDEX VALUE # | |
######################## | |
result = df.iloc[0:3] | |
result = df.iloc[0:2,0:2] | |
result = df.iloc[:3,:2] | |
df['Column4'] = pd.Series(randn(3),['A','B','C']) | |
df['Column5'] = df['Column2'] + df['Column3'] | |
# DF DROP # | |
# Silmek istediğimiz kolonu sileriz. | |
df.drop('Column5',axis= 1) # Column1'in yukarıdan aşağı olduğunu axis=1 ile belirtmemiz gerekiyor. | |
# DF'yi eşitleyerek güncellemek yerine, 'incplace = True' komuduyla otomatik olarak güncelleyebiliriz. | |
# df.drop('Column4',axis= 1, inplace= True) | |
result = df.drop('Column1', axis = 1, inplace = False) | |
print(df) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment