preloader

Funcionalidades de resumen de DataFrames

Índice de contenido
python
import pandas as pd
python
file_path = "C:/Users/user/Desktop/adult.data"

col_names = ["age", 
            "workclass",
            "fnlwgt", 
            "education",
            "education-num",
            "marital-status",
            "occupation",
            "relationship",
            "race",
            "sex",
            "capital-gain",
            "capital-loss",
            "hours-per-week",
            "native-country",
            "income"]


df = pd.read_csv(filepath_or_buffer = file_path,
           header = 0,
           names = col_names)
df

ageworkclassfnlwgteducationeducation-nummarital-statusoccupationrelationshipracesexcapital-gaincapital-losshours-per-weeknative-countryincome
050NaN83311Bachelors13Married-civ-spouseExec-managerialHusbandWhiteMale0013United-States<=50K
138Private215646HS-grad9DivorcedHandlers-cleanersNot-in-familyWhiteMale0040United-States<=50K
253Private23472111th7Married-civ-spouseHandlers-cleanersHusbandBlackMale0040United-States<=50K
328Private338409Bachelors13Married-civ-spouseProf-specialtyWifeBlackFemale0040Cuba<=50K
437Private284582Masters14Married-civ-spouseExec-managerialWifeWhiteFemale0040United-States<=50K
................................................
3255527Private257302Assoc-acdm12Married-civ-spouseTech-supportWifeWhiteFemale0038United-States<=50K
3255640Private154374HS-grad9Married-civ-spouseMachine-op-inspctHusbandWhiteMale0040United-States>50K
3255758Private151910HS-grad9WidowedAdm-clericalUnmarriedWhiteFemale0040United-States<=50K
3255822Private201490HS-grad9Never-marriedAdm-clericalOwn-childWhiteMale0020United-States<=50K
3255952Self-emp-inc287927HS-grad9Married-civ-spouseExec-managerialWifeWhiteFemale15024040United-States>50K

32560 rows × 15 columns

df.dtypes

python
df.dtypes
age                int64
workclass         object
fnlwgt             int64
education         object
education-num      int64
marital-status    object
occupation        object
relationship      object
race              object
sex               object
capital-gain       int64
capital-loss       int64
hours-per-week     int64
native-country    object
income            object
dtype: object

df.values

python
df.values
array([[50, nan, 83311, ..., 13, ' United-States', ' <=50K'],
       [38, ' Private', 215646, ..., 40, ' United-States', ' <=50K'],
       [53, ' Private', 234721, ..., 40, ' United-States', ' <=50K'],
       ...,
       [58, ' Private', 151910, ..., 40, ' United-States', ' <=50K'],
       [22, ' Private', 201490, ..., 20, ' United-States', ' <=50K'],
       [52, ' Self-emp-inc', 287927, ..., 40, ' United-States', ' >50K']],
      dtype=object)

df.ndim

python
df.ndim
2

df.shape

python
df.shape
(32560, 15)

df.size

python
df.size
488400

unique

python
df.apply(lambda x: x.unique())
age               [50, 38, 53, 28, 37, 49, 52, 31, 42, 30, 23, 3...
workclass         [nan,  Private,  Self-emp-not-inc,  State-gov,...
fnlwgt            [83311, 215646, 234721, 338409, 284582, 160187...
education         [ Bachelors,  HS-grad,  11th,  Masters,  9th, ...
education-num     [13, 9, 7, 14, 5, 10, 12, 11, 4, 16, 15, 3, 6,...
marital-status    [ Married-civ-spouse,  Divorced,  Married-spou...
occupation        [ Exec-managerial,  Handlers-cleaners,  Prof-s...
relationship      [ Husband,  Not-in-family,  Wife,  Own-child, ...
race              [ White,  Black,  Asian-Pac-Islander,  Amer-In...
sex                                                [ Male,  Female]
capital-gain      [0, 14084, 5178, 5013, 2407, 14344, 15024, 768...
capital-loss      [0, 2042, 1408, 1902, 1573, 1887, 1719, 1762, ...
hours-per-week    [13, 40, 16, 45, 50, 80, 30, 35, 60, 20, 52, 4...
native-country    [ United-States,  Cuba,  Jamaica,  India,  ?, ...
income                                              [ <=50K,  >50K]
dtype: object
python
df.workclass.unique()
array([nan, ' Private', ' Self-emp-not-inc', ' State-gov', ' Federal-gov',
       ' Local-gov', ' ?', ' Self-emp-inc', ' Without-pay',
       ' Never-worked'], dtype=object)

df.nunique

python
df.nunique(axis=0, dropna=True)
age                  73
workclass             9
fnlwgt            21647
education            16
education-num        16
marital-status        7
occupation           15
relationship          6
race                  5
sex                   2
capital-gain        119
capital-loss         92
hours-per-week       94
native-country       42
income                2
dtype: int64

df.max

python
df.max(axis = 0)
age                              90
fnlwgt                      1484705
education              Some-college
education-num                    16
marital-status              Widowed
occupation         Transport-moving
relationship                   Wife
race                          White
sex                            Male
capital-gain                  99999
capital-loss                   4356
hours-per-week                   99
native-country           Yugoslavia
income                         >50K
dtype: object

df.min

python
df.min(axis=0)
age                                17
fnlwgt                          12285
education                        10th
education-num                       1
marital-status               Divorced
occupation                          ?
relationship                  Husband
race               Amer-Indian-Eskimo
sex                            Female
capital-gain                        0
capital-loss                        0
hours-per-week                      1
native-country                      ?
income                          <=50K
dtype: object

df.duplicated

python
df.duplicated(subset = ["marital-status"])
0        False
1        False
2         True
3         True
4         True
         ...  
32555     True
32556     True
32557     True
32558     True
32559     True
Length: 32560, dtype: bool
python
df[df.duplicated(subset = ["age", "marital-status"])]

ageworkclassfnlwgteducationeducation-nummarital-statusoccupationrelationshipracesexcapital-gaincapital-losshours-per-weeknative-countryincome
937Private280464Some-college10Married-civ-spouseExec-managerialHusbandBlackMale0080United-States>50K
1632Private186824HS-grad9Never-marriedMachine-op-inspctUnmarriedWhiteMale0040United-States<=50K
1940Private193524Doctorate16Married-civ-spouseProf-specialtyHusbandWhiteMale0060United-States>50K
2923Local-gov190709Assoc-acdm12Never-marriedProtective-servNot-in-familyWhiteMale0052United-States<=50K
3230Federal-gov59951Some-college10Married-civ-spouseAdm-clericalOwn-childWhiteMale0040United-States<=50K
................................................
3255527Private257302Assoc-acdm12Married-civ-spouseTech-supportWifeWhiteFemale0038United-States<=50K
3255640Private154374HS-grad9Married-civ-spouseMachine-op-inspctHusbandWhiteMale0040United-States>50K
3255758Private151910HS-grad9WidowedAdm-clericalUnmarriedWhiteFemale0040United-States<=50K
3255822Private201490HS-grad9Never-marriedAdm-clericalOwn-childWhiteMale0020United-States<=50K
3255952Self-emp-inc287927HS-grad9Married-civ-spouseExec-managerialWifeWhiteFemale15024040United-States>50K

32164 rows × 15 columns

df.info()

python
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 32560 entries, 0 to 32559
Data columns (total 15 columns):
 #   Column          Non-Null Count  Dtype 
---  ------          --------------  ----- 
 0   age             32560 non-null  int64 
 1   workclass       32559 non-null  object
 2   fnlwgt          32560 non-null  int64 
 3   education       32560 non-null  object
 4   education-num   32560 non-null  int64 
 5   marital-status  32560 non-null  object
 6   occupation      32560 non-null  object
 7   relationship    32560 non-null  object
 8   race            32560 non-null  object
 9   sex             32560 non-null  object
 10  capital-gain    32560 non-null  int64 
 11  capital-loss    32560 non-null  int64 
 12  hours-per-week  32560 non-null  int64 
 13  native-country  32560 non-null  object
 14  income          32560 non-null  object
dtypes: int64(6), object(9)
memory usage: 3.7+ MB

df.describe()

python
df.describe(include = "all")

ageworkclassfnlwgteducationeducation-nummarital-statusoccupationrelationshipracesexcapital-gaincapital-losshours-per-weeknative-countryincome
count32560.000000325593.256000e+043256032560.000000325603256032560325603256032560.00000032560.00000032560.0000003256032560
uniqueNaN9NaN16NaN715652NaNNaNNaN422
topNaNPrivateNaNHS-gradNaNMarried-civ-spouseProf-specialtyHusbandWhiteMaleNaNNaNNaNUnited-States<=50K
freqNaN22696NaN10501NaN149764140131932781521789NaNNaNNaN2916924719
mean38.581634NaN1.897818e+05NaN10.080590NaNNaNNaNNaNNaN1077.61517287.30651140.437469NaNNaN
std13.640642NaN1.055498e+05NaN2.572709NaNNaNNaNNaNNaN7385.402999402.96611612.347618NaNNaN
min17.000000NaN1.228500e+04NaN1.000000NaNNaNNaNNaNNaN0.0000000.0000001.000000NaNNaN
25%28.000000NaN1.178315e+05NaN9.000000NaNNaNNaNNaNNaN0.0000000.00000040.000000NaNNaN
50%37.000000NaN1.783630e+05NaN10.000000NaNNaNNaNNaNNaN0.0000000.00000040.000000NaNNaN
75%48.000000NaN2.370545e+05NaN12.000000NaNNaNNaNNaNNaN0.0000000.00000045.000000NaNNaN
max90.000000NaN1.484705e+06NaN16.000000NaNNaNNaNNaNNaN99999.0000004356.00000099.000000NaNNaN
comments powered by Disqus