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
| age | workclass | fnlwgt | education | education-num | marital-status | occupation | relationship | race | sex | capital-gain | capital-loss | hours-per-week | native-country | income | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 50 | NaN | 83311 | Bachelors | 13 | Married-civ-spouse | Exec-managerial | Husband | White | Male | 0 | 0 | 13 | United-States | <=50K |
| 1 | 38 | Private | 215646 | HS-grad | 9 | Divorced | Handlers-cleaners | Not-in-family | White | Male | 0 | 0 | 40 | United-States | <=50K |
| 2 | 53 | Private | 234721 | 11th | 7 | Married-civ-spouse | Handlers-cleaners | Husband | Black | Male | 0 | 0 | 40 | United-States | <=50K |
| 3 | 28 | Private | 338409 | Bachelors | 13 | Married-civ-spouse | Prof-specialty | Wife | Black | Female | 0 | 0 | 40 | Cuba | <=50K |
| 4 | 37 | Private | 284582 | Masters | 14 | Married-civ-spouse | Exec-managerial | Wife | White | Female | 0 | 0 | 40 | United-States | <=50K |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 32555 | 27 | Private | 257302 | Assoc-acdm | 12 | Married-civ-spouse | Tech-support | Wife | White | Female | 0 | 0 | 38 | United-States | <=50K |
| 32556 | 40 | Private | 154374 | HS-grad | 9 | Married-civ-spouse | Machine-op-inspct | Husband | White | Male | 0 | 0 | 40 | United-States | >50K |
| 32557 | 58 | Private | 151910 | HS-grad | 9 | Widowed | Adm-clerical | Unmarried | White | Female | 0 | 0 | 40 | United-States | <=50K |
| 32558 | 22 | Private | 201490 | HS-grad | 9 | Never-married | Adm-clerical | Own-child | White | Male | 0 | 0 | 20 | United-States | <=50K |
| 32559 | 52 | Self-emp-inc | 287927 | HS-grad | 9 | Married-civ-spouse | Exec-managerial | Wife | White | Female | 15024 | 0 | 40 | United-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"])]
| age | workclass | fnlwgt | education | education-num | marital-status | occupation | relationship | race | sex | capital-gain | capital-loss | hours-per-week | native-country | income | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 9 | 37 | Private | 280464 | Some-college | 10 | Married-civ-spouse | Exec-managerial | Husband | Black | Male | 0 | 0 | 80 | United-States | >50K |
| 16 | 32 | Private | 186824 | HS-grad | 9 | Never-married | Machine-op-inspct | Unmarried | White | Male | 0 | 0 | 40 | United-States | <=50K |
| 19 | 40 | Private | 193524 | Doctorate | 16 | Married-civ-spouse | Prof-specialty | Husband | White | Male | 0 | 0 | 60 | United-States | >50K |
| 29 | 23 | Local-gov | 190709 | Assoc-acdm | 12 | Never-married | Protective-serv | Not-in-family | White | Male | 0 | 0 | 52 | United-States | <=50K |
| 32 | 30 | Federal-gov | 59951 | Some-college | 10 | Married-civ-spouse | Adm-clerical | Own-child | White | Male | 0 | 0 | 40 | United-States | <=50K |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 32555 | 27 | Private | 257302 | Assoc-acdm | 12 | Married-civ-spouse | Tech-support | Wife | White | Female | 0 | 0 | 38 | United-States | <=50K |
| 32556 | 40 | Private | 154374 | HS-grad | 9 | Married-civ-spouse | Machine-op-inspct | Husband | White | Male | 0 | 0 | 40 | United-States | >50K |
| 32557 | 58 | Private | 151910 | HS-grad | 9 | Widowed | Adm-clerical | Unmarried | White | Female | 0 | 0 | 40 | United-States | <=50K |
| 32558 | 22 | Private | 201490 | HS-grad | 9 | Never-married | Adm-clerical | Own-child | White | Male | 0 | 0 | 20 | United-States | <=50K |
| 32559 | 52 | Self-emp-inc | 287927 | HS-grad | 9 | Married-civ-spouse | Exec-managerial | Wife | White | Female | 15024 | 0 | 40 | United-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")
| age | workclass | fnlwgt | education | education-num | marital-status | occupation | relationship | race | sex | capital-gain | capital-loss | hours-per-week | native-country | income | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| count | 32560.000000 | 32559 | 3.256000e+04 | 32560 | 32560.000000 | 32560 | 32560 | 32560 | 32560 | 32560 | 32560.000000 | 32560.000000 | 32560.000000 | 32560 | 32560 |
| unique | NaN | 9 | NaN | 16 | NaN | 7 | 15 | 6 | 5 | 2 | NaN | NaN | NaN | 42 | 2 |
| top | NaN | Private | NaN | HS-grad | NaN | Married-civ-spouse | Prof-specialty | Husband | White | Male | NaN | NaN | NaN | United-States | <=50K |
| freq | NaN | 22696 | NaN | 10501 | NaN | 14976 | 4140 | 13193 | 27815 | 21789 | NaN | NaN | NaN | 29169 | 24719 |
| mean | 38.581634 | NaN | 1.897818e+05 | NaN | 10.080590 | NaN | NaN | NaN | NaN | NaN | 1077.615172 | 87.306511 | 40.437469 | NaN | NaN |
| std | 13.640642 | NaN | 1.055498e+05 | NaN | 2.572709 | NaN | NaN | NaN | NaN | NaN | 7385.402999 | 402.966116 | 12.347618 | NaN | NaN |
| min | 17.000000 | NaN | 1.228500e+04 | NaN | 1.000000 | NaN | NaN | NaN | NaN | NaN | 0.000000 | 0.000000 | 1.000000 | NaN | NaN |
| 25% | 28.000000 | NaN | 1.178315e+05 | NaN | 9.000000 | NaN | NaN | NaN | NaN | NaN | 0.000000 | 0.000000 | 40.000000 | NaN | NaN |
| 50% | 37.000000 | NaN | 1.783630e+05 | NaN | 10.000000 | NaN | NaN | NaN | NaN | NaN | 0.000000 | 0.000000 | 40.000000 | NaN | NaN |
| 75% | 48.000000 | NaN | 2.370545e+05 | NaN | 12.000000 | NaN | NaN | NaN | NaN | NaN | 0.000000 | 0.000000 | 45.000000 | NaN | NaN |
| max | 90.000000 | NaN | 1.484705e+06 | NaN | 16.000000 | NaN | NaN | NaN | NaN | NaN | 99999.000000 | 4356.000000 | 99.000000 | NaN | NaN |
