You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4244 lines
160 KiB
4244 lines
160 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "rQ8UhzFpgRra"
|
|
},
|
|
"source": [
|
|
"# డేటా తయారీ\n",
|
|
"\n",
|
|
"[మూల నోట్బుక్ మూలం *డేటా సైన్స్: డేటా సైన్స్ పైథాన్ మరియు మెషీన్ లెర్నింగ్ స్టూడియో కోసం మెషీన్ లెర్నింగ్ పరిచయం లీ స్టాట్* నుండి](https://github.com/leestott/intro-Datascience/blob/master/Course%20Materials/4-Cleaning_and_Manipulating-Reference.ipynb)\n",
|
|
"\n",
|
|
"## `DataFrame` సమాచారం అన్వేషణ\n",
|
|
"\n",
|
|
"> **అభ్యాస లక్ష్యం:** ఈ ఉపవిభాగం చివరికి, మీరు pandas DataFrames లో నిల్వ చేసిన డేటా గురించి సాధారణ సమాచారాన్ని కనుగొనడంలో సౌకర్యంగా ఉండాలి.\n",
|
|
"\n",
|
|
"మీరు మీ డేటాను pandas లో లోడ్ చేసిన తర్వాత, అది ఎక్కువగా `DataFrame` లోనే ఉంటుంది. అయితే, మీ `DataFrame` లో 60,000 వరుసలు మరియు 400 కాలమ్స్ ఉంటే, మీరు ఏ విధంగా మీరు పని చేస్తున్నదానిపై అవగాహన పొందడం ప్రారంభిస్తారు? అదృష్టవశాత్తు, pandas కొన్ని సౌకర్యవంతమైన సాధనాలను అందిస్తుంది, అవి `DataFrame` గురించి మొత్తం సమాచారాన్ని త్వరగా చూడటానికి, మొదటి కొన్ని మరియు చివరి కొన్ని వరుసలతో పాటు.\n",
|
|
"\n",
|
|
"ఈ ఫంక్షనాలిటీని అన్వేషించడానికి, మనం Python scikit-learn లైబ్రరీని దిగుమతి చేసుకుని, ప్రతి డేటా శాస్త్రవేత్త వందల సార్లు చూసిన ఒక ప్రతిష్టాత్మక డేటాసెట్ను ఉపయోగిస్తాము: బ్రిటిష్ బయాలజిస్ట్ రోనాల్డ్ ఫిషర్ యొక్క *Iris* డేటా సెట్, అతని 1936 పత్రంలో \"టాక్సోనామిక్ సమస్యలలో బహుళ కొలతల ఉపయోగం\" లో ఉపయోగించినది:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "hB1RofhdgRrp",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"from sklearn.datasets import load_iris\n",
|
|
"\n",
|
|
"iris = load_iris()\n",
|
|
"iris_df = pd.DataFrame(data=iris['data'], columns=iris['feature_names'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "AGA0A_Y8hMdz"
|
|
},
|
|
"source": [
|
|
"### `DataFrame.shape`\n",
|
|
"మేము `iris_df` అనే వేరియబుల్లో ఐరిస్ డేటాసెట్ను లోడ్ చేసుకున్నాము. డేటాలోకి దిగేముందు, మన దగ్గర ఉన్న డేటాపాయింట్ల సంఖ్య మరియు డేటాసెట్ యొక్క మొత్తం పరిమాణం తెలుసుకోవడం విలువైనది. మనం వ్యవహరిస్తున్న డేటా పరిమాణాన్ని చూడటం ఉపయోగకరం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "LOe5jQohhulf",
|
|
"outputId": "fb0577ac-3b4a-4623-cb41-20e1b264b3e9"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(150, 4)"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"iris_df.shape"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "smE7AGzOhxk2"
|
|
},
|
|
"source": [
|
|
"కాబట్టి, మనం 150 వరుసలు మరియు 4 కాలమ్స్ ఉన్న డేటాతో వ్యవహరిస్తున్నాము. ప్రతి వరుస ఒక డేటాపాయింట్ను సూచిస్తుంది మరియు ప్రతి కాలమ్ డేటా ఫ్రేమ్కు సంబంధించిన ఒక ఫీచర్ను సూచిస్తుంది. కాబట్టి, మౌలికంగా, ప్రతి ఒక్కటి 4 ఫీచర్లను కలిగి ఉన్న 150 డేటాపాయింట్లు ఉన్నాయి.\n",
|
|
"\n",
|
|
"`shape` ఇక్కడ డేటాఫ్రేమ్ యొక్క ఒక లక్షణం మరియు ఫంక్షన్ కాదు, అందుకే ఇది కౌన్సుల జంటతో ముగియదు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "d3AZKs0PinGP"
|
|
},
|
|
"source": [
|
|
"### `DataFrame.columns`\n",
|
|
"ఇప్పుడు మనం 4 డేటా కాలమ్స్ లోకి వెళదాం. వాటిలో ప్రతి ఒక్కటి ఖచ్చితంగా ఏమి సూచిస్తుంది? `columns` అట్రిబ్యూట్ డేటాఫ్రేమ్ లోని కాలమ్స్ పేరును మనకు ఇస్తుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "YPGh_ziji-CY",
|
|
"outputId": "74e7a43a-77cc-4c80-da56-7f50767c37a0"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Index(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',\n",
|
|
" 'petal width (cm)'],\n",
|
|
" dtype='object')"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"iris_df.columns"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "TsobcU_VjCC_"
|
|
},
|
|
"source": [
|
|
"మనము చూడగలిగినట్లుగా, నాలుగు(4) కాలమ్స్ ఉన్నాయి. `columns` లక్షణం కాలమ్స్ పేరును మనకు తెలియజేస్తుంది మరియు మిగతా ఏమీ కాదు. ఈ లక్షణం ఒక డేటాసెట్ లో ఏ లక్షణాలు ఉన్నాయో గుర్తించాలనుకున్నప్పుడు ప్రాముఖ్యత పొందుతుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "2UTlvkjmgRrs"
|
|
},
|
|
"source": [
|
|
"### `DataFrame.info`\n",
|
|
"డేటా పరిమాణం(`shape` లక్షణం ద్వారా ఇవ్వబడింది) మరియు ఫీచర్లు లేదా కాలమ్స్ పేర్లు(`columns` లక్షణం ద్వారా ఇవ్వబడింది) dataset గురించి మనకు ఏదో తెలియజేస్తాయి. ఇప్పుడు, మనం dataset లో మరింత లోతుగా వెళ్ళాలనుకుంటున్నాము. `DataFrame.info()` ఫంక్షన్ దీనికి చాలా ఉపయోగకరం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "dHHRyG0_gRrt",
|
|
"outputId": "d8fb0c40-4f18-4e19-da48-c8db77d1d3a5",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'pandas.core.frame.DataFrame'>\n",
|
|
"RangeIndex: 150 entries, 0 to 149\n",
|
|
"Data columns (total 4 columns):\n",
|
|
" # Column Non-Null Count Dtype \n",
|
|
"--- ------ -------------- ----- \n",
|
|
" 0 sepal length (cm) 150 non-null float64\n",
|
|
" 1 sepal width (cm) 150 non-null float64\n",
|
|
" 2 petal length (cm) 150 non-null float64\n",
|
|
" 3 petal width (cm) 150 non-null float64\n",
|
|
"dtypes: float64(4)\n",
|
|
"memory usage: 4.8 KB\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"iris_df.info()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "1XgVMpvigRru"
|
|
},
|
|
"source": [
|
|
"ఇక్కడి నుండి, మనం కొన్ని పరిశీలనలు చేయవచ్చు:\n",
|
|
"1. ప్రతి కాలమ్ యొక్క డేటాటైపు: ఈ డేటాసెట్లో, అన్ని డేటా 64-బిట్ ఫ్లోటింగ్-పాయింట్ సంఖ్యలుగా నిల్వ చేయబడింది.\n",
|
|
"2. నాన్-నల్ విలువల సంఖ్య: నల్ విలువలతో వ్యవహరించడం డేటా సిద్ధతలో ఒక ముఖ్యమైన దశ. ఇది నోట్బుక్లో తరువాత చర్చించబడుతుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "IYlyxbpWFEF4"
|
|
},
|
|
"source": [
|
|
"### DataFrame.describe()\n",
|
|
"మన డేటాసెట్లో చాలా సంఖ్యాత్మక డేటా ఉందని అనుకుందాం. సగటు, మధ్య విలువ, క్వార్టైల్లు మొదలైన ఒకవిధమైన గణాంక లెక్కింపులు ప్రతి కాలమ్పై వ్యక్తిగతంగా చేయవచ్చు. `DataFrame.describe()` ఫంక్షన్ డేటాసెట్లోని సంఖ్యాత్మక కాలమ్ల గణాంక సారాంశాన్ని అందిస్తుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 297
|
|
},
|
|
"id": "tWV-CMstFIRA",
|
|
"outputId": "4fc49941-bc13-4b0c-a412-cb39e7d3f289"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>sepal length (cm)</th>\n",
|
|
" <th>sepal width (cm)</th>\n",
|
|
" <th>petal length (cm)</th>\n",
|
|
" <th>petal width (cm)</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>count</th>\n",
|
|
" <td>150.000000</td>\n",
|
|
" <td>150.000000</td>\n",
|
|
" <td>150.000000</td>\n",
|
|
" <td>150.000000</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>mean</th>\n",
|
|
" <td>5.843333</td>\n",
|
|
" <td>3.057333</td>\n",
|
|
" <td>3.758000</td>\n",
|
|
" <td>1.199333</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>std</th>\n",
|
|
" <td>0.828066</td>\n",
|
|
" <td>0.435866</td>\n",
|
|
" <td>1.765298</td>\n",
|
|
" <td>0.762238</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>min</th>\n",
|
|
" <td>4.300000</td>\n",
|
|
" <td>2.000000</td>\n",
|
|
" <td>1.000000</td>\n",
|
|
" <td>0.100000</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>25%</th>\n",
|
|
" <td>5.100000</td>\n",
|
|
" <td>2.800000</td>\n",
|
|
" <td>1.600000</td>\n",
|
|
" <td>0.300000</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>50%</th>\n",
|
|
" <td>5.800000</td>\n",
|
|
" <td>3.000000</td>\n",
|
|
" <td>4.350000</td>\n",
|
|
" <td>1.300000</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>75%</th>\n",
|
|
" <td>6.400000</td>\n",
|
|
" <td>3.300000</td>\n",
|
|
" <td>5.100000</td>\n",
|
|
" <td>1.800000</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>max</th>\n",
|
|
" <td>7.900000</td>\n",
|
|
" <td>4.400000</td>\n",
|
|
" <td>6.900000</td>\n",
|
|
" <td>2.500000</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
|
|
"count 150.000000 150.000000 150.000000 150.000000\n",
|
|
"mean 5.843333 3.057333 3.758000 1.199333\n",
|
|
"std 0.828066 0.435866 1.765298 0.762238\n",
|
|
"min 4.300000 2.000000 1.000000 0.100000\n",
|
|
"25% 5.100000 2.800000 1.600000 0.300000\n",
|
|
"50% 5.800000 3.000000 4.350000 1.300000\n",
|
|
"75% 6.400000 3.300000 5.100000 1.800000\n",
|
|
"max 7.900000 4.400000 6.900000 2.500000"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"iris_df.describe()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "zjjtW5hPGMuM"
|
|
},
|
|
"source": [
|
|
"పై అవుట్పుట్ ప్రతి కాలమ్ యొక్క మొత్తం డేటా పాయింట్లు, సగటు, ప్రమాణ విభిన్నత, కనిష్ఠం, తక్కువ క్వార్టైల్(25%), మధ్యస్థానం(50%), పై క్వార్టైల్(75%) మరియు గరిష్ఠ విలువను చూపిస్తుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "-lviAu99gRrv"
|
|
},
|
|
"source": [
|
|
"### `DataFrame.head`\n",
|
|
"పై అన్ని ఫంక్షన్లు మరియు లక్షణాలతో, మేము డేటాసెట్ యొక్క టాప్ లెవల్ వీక్షణను పొందాము. ఎన్ని డేటా పాయింట్లు ఉన్నాయో, ఎన్ని ఫీచర్లు ఉన్నాయో, ప్రతి ఫీచర్ యొక్క డేటా రకం మరియు ప్రతి ఫీచర్ కోసం ఎన్ని నాన్-నల్ విలువలు ఉన్నాయో మాకు తెలుసు.\n",
|
|
"\n",
|
|
"ఇప్పుడు డేటాను స్వయంగా చూడాల్సిన సమయం వచ్చింది. మన `DataFrame` యొక్క మొదటి కొన్ని వరుసలు (మొదటి కొన్ని డేటాపాయింట్లు) ఎలా ఉన్నాయో చూద్దాం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "DZMJZh0OgRrw",
|
|
"outputId": "d9393ee5-c106-4797-f815-218f17160e00",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>sepal length (cm)</th>\n",
|
|
" <th>sepal width (cm)</th>\n",
|
|
" <th>petal length (cm)</th>\n",
|
|
" <th>petal width (cm)</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>5.1</td>\n",
|
|
" <td>3.5</td>\n",
|
|
" <td>1.4</td>\n",
|
|
" <td>0.2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>4.9</td>\n",
|
|
" <td>3.0</td>\n",
|
|
" <td>1.4</td>\n",
|
|
" <td>0.2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>4.7</td>\n",
|
|
" <td>3.2</td>\n",
|
|
" <td>1.3</td>\n",
|
|
" <td>0.2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>4.6</td>\n",
|
|
" <td>3.1</td>\n",
|
|
" <td>1.5</td>\n",
|
|
" <td>0.2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>3.6</td>\n",
|
|
" <td>1.4</td>\n",
|
|
" <td>0.2</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
|
|
"0 5.1 3.5 1.4 0.2\n",
|
|
"1 4.9 3.0 1.4 0.2\n",
|
|
"2 4.7 3.2 1.3 0.2\n",
|
|
"3 4.6 3.1 1.5 0.2\n",
|
|
"4 5.0 3.6 1.4 0.2"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"iris_df.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "EBHEimZuEFQK"
|
|
},
|
|
"source": [
|
|
"ఇక్కడ అవుట్పుట్గా, మనం డేటాసెట్లో ఐదు(5) ఎంట్రీలను చూడవచ్చు. ఎడమవైపు ఉన్న సూచికను చూస్తే, ఇవి మొదటి ఐదు వరుసలు అని మనం తెలుసుకోవచ్చు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "oj7GkrTdgRry"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n",
|
|
"\n",
|
|
"పై ఉదాహరణ నుండి, డిఫాల్ట్గా, `DataFrame.head` ఒక `DataFrame` యొక్క మొదటి ఐదు వరుసలను తిరిగి ఇస్తుంది అని స్పష్టంగా తెలుస్తోంది. క్రింది కోడ్ సెల్లో, మీరు ఐదు వరుసల కంటే ఎక్కువ వరుసలను ప్రదర్శించడానికి ఒక మార్గాన్ని కనుగొనగలరా?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "EKRmRFFegRrz",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Hint: Consult the documentation by using iris_df.head?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "BJ_cpZqNgRr1"
|
|
},
|
|
"source": [
|
|
"### `DataFrame.tail`\n",
|
|
"డేటాను చూడటానికి మరో మార్గం ప్రారంభం కాకుండా చివర నుండి చూడటం. `DataFrame.head` యొక్క విరుద్ధం `DataFrame.tail`, ఇది ఒక `DataFrame` యొక్క చివరి ఐదు వరుసలను తిరిగి ఇస్తుంది:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 0
|
|
},
|
|
"id": "heanjfGWgRr2",
|
|
"outputId": "6ae09a21-fe09-4110-b0d7-1a1fbf34d7f3",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>sepal length (cm)</th>\n",
|
|
" <th>sepal width (cm)</th>\n",
|
|
" <th>petal length (cm)</th>\n",
|
|
" <th>petal width (cm)</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>145</th>\n",
|
|
" <td>6.7</td>\n",
|
|
" <td>3.0</td>\n",
|
|
" <td>5.2</td>\n",
|
|
" <td>2.3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>146</th>\n",
|
|
" <td>6.3</td>\n",
|
|
" <td>2.5</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>1.9</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>147</th>\n",
|
|
" <td>6.5</td>\n",
|
|
" <td>3.0</td>\n",
|
|
" <td>5.2</td>\n",
|
|
" <td>2.0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>148</th>\n",
|
|
" <td>6.2</td>\n",
|
|
" <td>3.4</td>\n",
|
|
" <td>5.4</td>\n",
|
|
" <td>2.3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>149</th>\n",
|
|
" <td>5.9</td>\n",
|
|
" <td>3.0</td>\n",
|
|
" <td>5.1</td>\n",
|
|
" <td>1.8</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
|
|
"145 6.7 3.0 5.2 2.3\n",
|
|
"146 6.3 2.5 5.0 1.9\n",
|
|
"147 6.5 3.0 5.2 2.0\n",
|
|
"148 6.2 3.4 5.4 2.3\n",
|
|
"149 5.9 3.0 5.1 1.8"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"iris_df.tail()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "31kBWfyLgRr3"
|
|
},
|
|
"source": [
|
|
"ప్రాక్టీస్లో, మీరు ఆర్డర్ చేయబడిన డేటాసెట్లలో అవుట్లయర్లను వెతుకుతున్నప్పుడు, `DataFrame` యొక్క మొదటి కొన్ని వరుసలు లేదా చివరి కొన్ని వరుసలను సులభంగా పరిశీలించగలగడం ఉపయోగకరం.\n",
|
|
"\n",
|
|
"పై కోడ్ ఉదాహరణల సహాయంతో చూపించిన అన్ని ఫంక్షన్లు మరియు లక్షణాలు, డేటాను చూడటానికి మరియు అనుభూతి పొందటానికి సహాయపడతాయి.\n",
|
|
"\n",
|
|
"> **Takeaway:** DataFrameలోని సమాచారంపై మెటాడేటాను లేదా దాని మొదటి మరియు చివరి కొన్ని విలువలను చూసే క్రమంలోనే, మీరు వ్యవహరిస్తున్న డేటా యొక్క పరిమాణం, ఆకారం మరియు విషయంపై తక్షణమే ఒక ఆలోచన పొందవచ్చు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "TvurZyLSDxq_"
|
|
},
|
|
"source": [
|
|
"### లేని డేటా\n",
|
|
"లేని డేటాలోకి మనం దిగుదాం. కొన్ని కాలమ్స్లో విలువ నిల్వ చేయబడకపోతే లేని డేటా సంభవిస్తుంది.\n",
|
|
"\n",
|
|
"ఒక ఉదాహరణ తీసుకుందాం: ఎవరో వారి బరువు గురించి జాగ్రత్తగా ఉంటే, సర్వేలో బరువు ఫీల్డ్ను నింపకపోవచ్చు. అప్పుడు ఆ వ్యక్తి కోసం బరువు విలువ లేనిది అవుతుంది.\n",
|
|
"\n",
|
|
"అధికంగా, వాస్తవ ప్రపంచ డేటాసెట్లలో, లేని విలువలు సంభవిస్తాయి.\n",
|
|
"\n",
|
|
"**పాండాస్ లేని డేటాను ఎలా నిర్వహిస్తుంది**\n",
|
|
"\n",
|
|
"\n",
|
|
"పాండాస్ లేని విలువలను రెండు విధాలుగా నిర్వహిస్తుంది. మీరు ఇంతకు ముందు చూసిన మొదటి విధానం: `NaN`, లేదా నాట్ ఎ నంబర్. ఇది వాస్తవానికి IEEE ఫ్లోటింగ్-పాయింట్ స్పెసిఫికేషన్లో భాగమైన ప్రత్యేక విలువ మరియు ఇది కేవలం లేని ఫ్లోటింగ్-పాయింట్ విలువలను సూచించడానికి ఉపయోగించబడుతుంది.\n",
|
|
"\n",
|
|
"ఫ్లోట్స్ కాకుండా లేని విలువల కోసం, పాండాస్ పైథాన్ `None` ఆబ్జెక్ట్ను ఉపయోగిస్తుంది. మీరు రెండు వేర్వేరు రకాల విలువలను ఎదుర్కొంటారని అనిపించవచ్చు, అవి సారాంశంగా అదే విషయాన్ని చెబుతున్నాయి, కానీ ఈ డిజైన్ ఎంపికకు బలమైన ప్రోగ్రామాటిక్ కారణాలు ఉన్నాయి మరియు, ప్రాక్టికల్గా, ఈ మార్గం పాండాస్కు చాలా సందర్భాల్లో మంచి సమతుల్యతను అందించడానికి సహాయపడుతుంది. అయినప్పటికీ, `None` మరియు `NaN` రెండూ ఉపయోగించడంలో మీరు జాగ్రత్తగా ఉండాల్సిన పరిమితులు కలిగి ఉంటాయి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "lOHqUlZFgRr5"
|
|
},
|
|
"source": [
|
|
"### `None`: ఫ్లోట్ కాని లేని డేటా\n",
|
|
"`None` పాథాన్ నుండి వచ్చినందున, ఇది `'object'` డేటా టైప్ కాని NumPy మరియు pandas అర్రేలలో ఉపయోగించలేము. గుర్తుంచుకోండి, NumPy అర్రేలు (మరియు pandas లోని డేటా నిర్మాణాలు) ఒకే రకమైన డేటాను మాత్రమే కలిగి ఉండగలవు. ఇది పెద్ద స్థాయి డేటా మరియు గణనాత్మక పనులకు వారికి గొప్ప శక్తిని ఇస్తుంది, కానీ ఇది వారి సౌలభ్యాన్ని పరిమితం చేస్తుంది. అలాంటి అర్రేలు \"కనిష్ట సాధారణ భాగస్వామి\"కి అప్కాస్ట్ చేయాలి, అంటే అర్రేలోని అన్ని అంశాలను కవర్ చేసే డేటా టైప్. అర్రేలో `None` ఉన్నప్పుడు, మీరు పాథాన్ ఆబ్జెక్టులతో పని చేస్తున్నారని అర్థం.\n",
|
|
"\n",
|
|
"దీనిని ప్రదర్శించడానికి, క్రింది ఉదాహరణ అర్రేను పరిగణించండి (దాని `dtype` గమనించండి):\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "QIoNdY4ngRr7",
|
|
"outputId": "92779f18-62f4-4a03-eca2-e9a101604336",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"array([2, None, 6, 8], dtype=object)"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"example1 = np.array([2, None, 6, 8])\n",
|
|
"example1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "pdlgPNbhgRr7"
|
|
},
|
|
"source": [
|
|
"అప్కాస్ట్ డేటా రకాల వాస్తవం రెండు పక్క ప్రభావాలను కలిగి ఉంటుంది. మొదటిది, ఆపరేషన్లు కంపైల్ చేయబడిన NumPy కోడ్ కంటే అనువాదం చేయబడిన Python కోడ్ స్థాయిలో నిర్వహించబడతాయి. సారాంశంగా, ఇది `None` ఉన్న `Series` లేదా `DataFrames`తో సంబంధం ఉన్న ఏ ఆపరేషన్లు అయినా నెమ్మదిగా జరుగుతాయని అర్థం. మీరు ఈ పనితీరు తగ్గుదలని గమనించకపోవచ్చు, కానీ పెద్ద డేటాసెట్ల కోసం ఇది సమస్యగా మారవచ్చు.\n",
|
|
"\n",
|
|
"రెండవ పక్క ప్రభావం మొదటి నుండి ఉద్భవిస్తుంది. ఎందుకంటే `None` అసలు `Series` లేదా `DataFrame`లను వనిల్లా Python ప్రపంచంలోకి తిరిగి తీసుకువస్తుంది, అందువల్ల `None` విలువ కలిగిన అర్రేలపై NumPy/pandas సమాహారాలు వంటి `sum()` లేదా `min()` ఉపయోగించడం సాధారణంగా ఒక లోపాన్ని ఉత్పత్తి చేస్తుంది:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 292
|
|
},
|
|
"id": "gWbx-KB9gRr8",
|
|
"outputId": "ecba710a-22ec-41d5-a39c-11f67e645b50",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "ignored",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[0;32m<ipython-input-10-ce9901ad18bd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mexample1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/numpy/core/_methods.py\u001b[0m in \u001b[0;36m_sum\u001b[0;34m(a, axis, dtype, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 45\u001b[0m def _sum(a, axis=None, dtype=None, out=None, keepdims=False,\n\u001b[1;32m 46\u001b[0m initial=_NoValue, where=True):\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mumr_sum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m def _prod(a, axis=None, dtype=None, out=None, keepdims=False,\n",
|
|
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'NoneType'"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"example1.sum()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "LcEwO8UogRr9"
|
|
},
|
|
"source": [
|
|
"**ప్రధాన విషయం**: పూర్తి సంఖ్యలు మరియు `None` విలువల మధ్య జోడింపు (మరియు ఇతర ఆపరేషన్లు) నిర్వచించబడలేదు, ఇది వాటిని కలిగిన డేటాసెట్లతో మీరు చేయగలిగే పనులను పరిమితం చేయవచ్చు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "pWvVHvETgRr9"
|
|
},
|
|
"source": [
|
|
"### `NaN`: లేని ఫ్లోట్ విలువలు\n",
|
|
"\n",
|
|
"`None` కు విరుద్ధంగా, NumPy (అందువల్ల pandas కూడా) దాని వేగవంతమైన, వెక్టరైజ్డ్ ఆపరేషన్లు మరియు ufuncs కోసం `NaN` ను మద్దతు ఇస్తుంది. చెడు వార్త ఏమిటంటే, `NaN` పై చేసిన ఏ గణిత చర్య అయినా ఎప్పుడూ `NaN` ను ఫలితంగా ఇస్తుంది. ఉదాహరణకు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "rcFYfMG9gRr9",
|
|
"outputId": "699e81b7-5c11-4b46-df1d-06071768690f",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"nan"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"np.nan + 1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "BW3zQD2-gRr-",
|
|
"outputId": "4525b6c4-495d-4f7b-a979-efce1dae9bd0",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"nan"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"np.nan * 0"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "fU5IPRcCgRr-"
|
|
},
|
|
"source": [
|
|
"మంచి వార్త: `NaN` ఉన్న అర్రేలపై అమర్చిన సమాహారాలు లోపాలు చూపించవు. చెడు వార్త: ఫలితాలు సమానంగా ఉపయోగకరంగా ఉండవు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "LCInVgSSgRr_",
|
|
"outputId": "fa06495a-0930-4867-87c5-6023031ea8b5",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(nan, nan, nan)"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example2 = np.array([2, np.nan, 6, 8]) \n",
|
|
"example2.sum(), example2.min(), example2.max()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "nhlnNJT7gRr_"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "yan3QRaOgRr_",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# What happens if you add np.nan and None together?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "_iDvIRC8gRsA"
|
|
},
|
|
"source": [
|
|
"గుర్తుంచుకోండి: `NaN` అనేది కేవలం లేని ఫ్లోటింగ్-పాయింట్ విలువల కోసం మాత్రమే; పూర్తి సంఖ్యలు, స్ట్రింగులు లేదా బూలియన్లకు `NaN` సమానమైనది లేదు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "kj6EKdsAgRsA"
|
|
},
|
|
"source": [
|
|
"### `NaN` మరియు `None`: pandas లో null విలువలు\n",
|
|
"\n",
|
|
"`NaN` మరియు `None` కొంతవరకు వేరుగా ప్రవర్తించగలిగినా, pandas వాటిని పరస్పరం మార్చుకోగలిగేలా నిర్మించబడింది. మనం అర్థం చేసుకోవడానికి, ఒక `Series` యొక్క పూర్తి సంఖ్యలను పరిగణించండి:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "Nji-KGdNgRsA",
|
|
"outputId": "36aa14d2-8efa-4bfd-c0ed-682991288822",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"0 1\n",
|
|
"1 2\n",
|
|
"2 3\n",
|
|
"dtype: int64"
|
|
]
|
|
},
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"int_series = pd.Series([1, 2, 3], dtype=int)\n",
|
|
"int_series"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "WklCzqb8gRsB"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "Cy-gqX5-gRsB",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Now set an element of int_series equal to None.\n",
|
|
"# How does that element show up in the Series?\n",
|
|
"# What is the dtype of the Series?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "WjMQwltNgRsB"
|
|
},
|
|
"source": [
|
|
"`Series` మరియు `DataFrame` లలో డేటా సమానత్వాన్ని స్థాపించడానికి డేటా రకాలని అప్కాస్టింగ్ చేసే ప్రక్రియలో, pandas `None` మరియు `NaN` మధ్య లోపాల విలువలను సులభంగా మార్చుకుంటుంది. ఈ డిజైన్ లక్షణం కారణంగా, pandas లో `None` మరియు `NaN` ను \"null\" యొక్క రెండు వేర్వేరు రుచులుగా భావించడం సహాయకరం. నిజానికి, pandas లో లోపాల విలువలతో వ్యవహరించడానికి మీరు ఉపయోగించే కొన్ని ప్రధాన పద్ధతులు వారి పేర్లలో ఈ ఆలోచనను ప్రతిబింబిస్తాయి:\n",
|
|
"\n",
|
|
"- `isnull()`: లోపాల విలువలను సూచించే బూలియన్ మాస్క్ను ఉత్పత్తి చేస్తుంది\n",
|
|
"- `notnull()`: `isnull()` కు వ్యతిరేకం\n",
|
|
"- `dropna()`: డేటా యొక్క ఫిల్టర్ చేయబడిన సంస్కరణను తిరిగి ఇస్తుంది\n",
|
|
"- `fillna()`: లోపాల విలువలు నింపబడిన లేదా అంచనా వేయబడిన డేటా కాపీని తిరిగి ఇస్తుంది\n",
|
|
"\n",
|
|
"ఇవి నేర్చుకోవడానికి మరియు సౌకర్యవంతంగా ఉపయోగించుకోవడానికి ముఖ్యమైన పద్ధతులు, కాబట్టి వాటిని ప్రతి ఒక్కదానిని కొంత లోతుగా పరిశీలిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Yh5ifd9FgRsB"
|
|
},
|
|
"source": [
|
|
"### null విలువలను గుర్తించడం\n",
|
|
"\n",
|
|
"ఇప్పుడు మేము మిస్సింగ్ విలువల ప్రాముఖ్యతను అర్థం చేసుకున్నాము, వాటిని మా డేటాసెట్లో గుర్తించాలి, వాటిని నిర్వహించే ముందు.\n",
|
|
"`isnull()` మరియు `notnull()` రెండూ null డేటాను గుర్తించడానికి మీ ప్రాథమిక పద్ధతులు. ఇవి రెండూ మీ డేటాపై బూలియన్ మాస్క్లను తిరిగి ఇస్తాయి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "e-vFp5lvgRsC",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"example3 = pd.Series([0, np.nan, '', None])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "1XdaJJ7PgRsC",
|
|
"outputId": "92fc363a-1874-471f-846d-f4f9ce1f51d0",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"0 False\n",
|
|
"1 True\n",
|
|
"2 False\n",
|
|
"3 True\n",
|
|
"dtype: bool"
|
|
]
|
|
},
|
|
"execution_count": 18,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example3.isnull()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "PaSZ0SQygRsC"
|
|
},
|
|
"source": [
|
|
"ఫలితాన్ని జాగ్రత్తగా పరిశీలించండి. దాని లో ఏదైనా మీకు ఆశ్చర్యంగా అనిపిస్తుందా? `0` ఒక గణిత శూన్యం అయినప్పటికీ, అది పూర్తిగా సరైన పూర్తి సంఖ్య మరియు pandas దాన్ని అలానే పరిగణిస్తుంది. `''` కొంచెం సున్నితమైనది. మేము దాన్ని సెక్షన్ 1 లో ఖాళీ స్ట్రింగ్ విలువను సూచించడానికి ఉపయోగించినప్పటికీ, అది pandas దృష్టిలో ఒక స్ట్రింగ్ ఆబ్జెక్ట్ మాత్రమే మరియు శూన్యం యొక్క ప్రాతినిధ్యం కాదు.\n",
|
|
"\n",
|
|
"ఇప్పుడు, దీన్ని తిరగబెట్టుకొని, మీరు ప్రాక్టీస్ లో ఉపయోగించే విధంగా ఈ పద్ధతులను ఉపయోగిద్దాం. మీరు Boolean మాస్క్ లను నేరుగా ``Series`` లేదా ``DataFrame`` సూచికగా ఉపయోగించవచ్చు, ఇది వేరుచేసిన లేని (లేదా ఉన్న) విలువలతో పని చేయడానికి ఉపయోగకరంగా ఉంటుంది.\n",
|
|
"\n",
|
|
"మేము మొత్తం లేని విలువల సంఖ్య తెలుసుకోవాలనుకుంటే, `isnull()` పద్ధతి ద్వారా ఉత్పత్తి అయిన మాస్క్ పై సుమ్ము చేయవచ్చు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "JCcQVoPkHDUv",
|
|
"outputId": "001daa72-54f8-4bd5-842a-4df627a79d4d"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"2"
|
|
]
|
|
},
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example3.isnull().sum()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "PlBqEo3mgRsC"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "ggDVf5uygRsD",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Try running example3[example3.notnull()].\n",
|
|
"# Before you do so, what do you expect to see?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "D_jWN7mHgRsD"
|
|
},
|
|
"source": [
|
|
"**ప్రధాన విషయం**: మీరు DataFrames లో `isnull()` మరియు `notnull()` పద్ధతులను ఉపయోగించినప్పుడు రెండూ సమాన ఫలితాలను ఇస్తాయి: అవి ఫలితాలను మరియు ఆ ఫలితాల సూచికను చూపిస్తాయి, ఇది మీ డేటాతో పోరాడేటప్పుడు మీకు చాలా సహాయం చేస్తుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "BvnoojWsgRr4"
|
|
},
|
|
"source": [
|
|
"### లేని డేటాతో వ్యవహరించడం\n",
|
|
"\n",
|
|
"> **అధ్యయన లక్ష్యం:** ఈ ఉపవిభాగం ముగిసే వరకు, మీరు DataFrames నుండి నల్ విలువలను ఎప్పుడు మరియు ఎలా మార్చాలి లేదా తొలగించాలి అనేది తెలుసుకోవాలి.\n",
|
|
"\n",
|
|
"మిషన్ లెర్నింగ్ మోడల్స్ స్వయంగా లేని డేటాతో వ్యవహరించలేవు. కాబట్టి, డేటాను మోడల్కు పంపించే ముందు, ఈ లేని విలువలతో వ్యవహరించాలి.\n",
|
|
"\n",
|
|
"లేని డేటా ఎలా నిర్వహించబడుతుందో దానితో సున్నితమైన వ్యాపారాలు ఉంటాయి, ఇది మీ తుది విశ్లేషణ మరియు వాస్తవ ప్రపంచ ఫలితాలను ప్రభావితం చేయవచ్చు.\n",
|
|
"\n",
|
|
"లేని డేటాతో వ్యవహరించడానికి ప్రధానంగా రెండు మార్గాలు ఉన్నాయి:\n",
|
|
"\n",
|
|
"\n",
|
|
"1. లేని విలువ ఉన్న వరుసను తొలగించండి\n",
|
|
"2. లేని విలువను మరొక విలువతో మార్చండి\n",
|
|
"\n",
|
|
"మేము ఈ రెండు పద్ధతులను మరియు వాటి లాభాలు మరియు నష్టాలను వివరంగా చర్చిస్తాము.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "3VaYC1TvgRsD"
|
|
},
|
|
"source": [
|
|
"### నల్ విలువలను తొలగించడం\n",
|
|
"\n",
|
|
"మనం మా మోడల్కు పంపే డేటా పరిమాణం దాని పనితీరుపై ప్రత్యక్ష ప్రభావం చూపుతుంది. నల్ విలువలను తొలగించడం అంటే మనం డేటాపాయింట్ల సంఖ్యను తగ్గిస్తున్నాము, అందువల్ల డేటాసెట్ పరిమాణం తగ్గుతుంది. కాబట్టి, డేటాసెట్ చాలా పెద్దదైతే నల్ విలువలతో ఉన్న వరుసలను తొలగించడం మంచిది.\n",
|
|
"\n",
|
|
"మరొక సందర్భం ఏమిటంటే, ఒక నిర్దిష్ట వరుస లేదా కాలమ్లో చాలా మిస్సింగ్ విలువలు ఉండవచ్చు. అప్పుడు, అవి తొలగించబడవచ్చు ఎందుకంటే ఆ వరుస/కాలమ్కు సంబంధించిన డేటా ఎక్కువగా లేనందున అవి మన విశ్లేషణకు ఎక్కువ విలువను చేర్చవు.\n",
|
|
"\n",
|
|
"మిస్సింగ్ విలువలను గుర్తించడం మించి, pandas `Series` మరియు `DataFrame` ల నుండి నల్ విలువలను తొలగించడానికి సౌకర్యవంతమైన మార్గాన్ని అందిస్తుంది. దీన్ని ప్రదర్శించడానికి, మనం `example3` కు తిరిగి వెళ్దాం. `DataFrame.dropna()` ఫంక్షన్ నల్ విలువలతో ఉన్న వరుసలను తొలగించడంలో సహాయపడుతుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "7uIvS097gRsD",
|
|
"outputId": "c13fc117-4ca1-4145-a0aa-42ac89e6e218",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"0 0\n",
|
|
"2 \n",
|
|
"dtype: object"
|
|
]
|
|
},
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example3 = example3.dropna()\n",
|
|
"example3"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "hil2cr64gRsD"
|
|
},
|
|
"source": [
|
|
"గమనించండి ఇది `example3[example3.notnull()]` నుండి మీ అవుట్పుట్ లాగా కనిపించాలి. ఇక్కడ తేడా ఏమిటంటే, మాస్క్ చేయబడిన విలువలపై మాత్రమే సూచిక వేయడం కాకుండా, `dropna` ఆ లేని విలువలను `Series` `example3` నుండి తొలగించింది.\n",
|
|
"\n",
|
|
"DataFrames కి రెండు కొలతలు ఉండటం వలన, డేటాను తొలగించడానికి మరిన్ని ఎంపికలు అందుబాటులో ఉంటాయి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "an-l74sPgRsE",
|
|
"outputId": "340876a0-63ad-40f6-bd54-6240cdae50ab",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>7</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>9</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 1.0 NaN 7\n",
|
|
"1 2.0 5.0 8\n",
|
|
"2 NaN 6.0 9"
|
|
]
|
|
},
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4 = pd.DataFrame([[1, np.nan, 7], \n",
|
|
" [2, 5, 8], \n",
|
|
" [np.nan, 6, 9]])\n",
|
|
"example4"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "66wwdHZrgRsE"
|
|
},
|
|
"source": [
|
|
"(పాండాస్ `NaN` లను అనుకూలించడానికి రెండు కాలమ్స్ను ఫ్లోట్స్గా అప్కాస్ట్ చేస్తున్నట్లు మీరు గమనించారా?)\n",
|
|
"\n",
|
|
"మీరు ఒకే విలువను `DataFrame` నుండి తొలగించలేరు, కాబట్టి మీరు పూర్తి వరుసలు లేదా కాలమ్స్ను తొలగించాలి. మీరు ఏమి చేస్తున్నారో ఆధారంగా, మీరు ఒకటి లేదా మరొకదాన్ని చేయాలనుకోవచ్చు, అందుకే పాండాస్ రెండు కోసం ఎంపికలను ఇస్తుంది. డేటా సైన్స్లో, కాలమ్స్ సాధారణంగా వేరియబుల్స్ను సూచిస్తాయి మరియు వరుసలు పరిశీలనలను సూచిస్తాయి, కాబట్టి మీరు ఎక్కువగా డేటా వరుసలను తొలగించే అవకాశం ఉంటుంది; `dropna()` యొక్క డిఫాల్ట్ సెట్టింగ్ ఏదైనా నల్ విలువలు ఉన్న అన్ని వరుసలను తొలగించడం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 80
|
|
},
|
|
"id": "jAVU24RXgRsE",
|
|
"outputId": "0b5e5aee-7187-4d3f-b583-a44136ae5f80",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"1 2.0 5.0 8"
|
|
]
|
|
},
|
|
"execution_count": 23,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4.dropna()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "TrQRBuTDgRsE"
|
|
},
|
|
"source": [
|
|
"అవసరమైతే, మీరు కాలమ్స్ నుండి NA విలువలను తొలగించవచ్చు. అలా చేయడానికి `axis=1` ఉపయోగించండి:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "GrBhxu9GgRsE",
|
|
"outputId": "ff4001f3-2e61-4509-d60e-0093d1068437",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>7</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>8</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>9</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 2\n",
|
|
"0 7\n",
|
|
"1 8\n",
|
|
"2 9"
|
|
]
|
|
},
|
|
"execution_count": 24,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4.dropna(axis='columns')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "KWXiKTfMgRsF"
|
|
},
|
|
"source": [
|
|
"గమనించండి, ఇది మీరు ఉంచాలనుకునే చాలా డేటాను డ్రాప్ చేయవచ్చు, ముఖ్యంగా చిన్న డేటాసెట్లలో. మీరు కేవలం కొన్ని లేదా అన్ని నల్ విలువలను కలిగిన వరుసలు లేదా కాలమ్స్ను మాత్రమే డ్రాప్ చేయాలనుకుంటే ఏమవుతుంది? మీరు `dropna` లో `how` మరియు `thresh` పారామీటర్లతో ఆ సెట్టింగ్స్ను నిర్దేశించవచ్చు.\n",
|
|
"\n",
|
|
"డిఫాల్ట్గా, `how='any'` ఉంటుంది (మీరు స్వయంగా తనిఖీ చేయాలనుకుంటే లేదా ఈ మెథడ్లో మరే ఇతర పారామీటర్లు ఉన్నాయా చూడాలనుకుంటే, కోడ్ సెల్లో `example4.dropna?` ను రన్ చేయండి). మీరు ప్రత్యామ్నాయంగా `how='all'` ను నిర్దేశించవచ్చు, తద్వారా కేవలం అన్ని నల్ విలువలను కలిగిన వరుసలు లేదా కాలమ్స్ మాత్రమే డ్రాప్ చేయబడతాయి. ఈ క్రింది వ్యాయామంలో ఈ చర్యను చూడటానికి మన ఉదాహరణ `DataFrame` ను విస్తరిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 25,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "Bcf_JWTsgRsF",
|
|
"outputId": "72e0b1b8-52fa-4923-98ce-b6fbed6e44b1",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" <th>3</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>7</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>9</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2 3\n",
|
|
"0 1.0 NaN 7 NaN\n",
|
|
"1 2.0 5.0 8 NaN\n",
|
|
"2 NaN 6.0 9 NaN"
|
|
]
|
|
},
|
|
"execution_count": 25,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4[3] = np.nan\n",
|
|
"example4"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "pNZer7q9JPNC"
|
|
},
|
|
"source": [
|
|
"> ముఖ్యాంశాలు: \n",
|
|
"1. డేటాసెట్ పెద్దదైతేనే నల్ విలువలను తొలగించడం మంచిది. \n",
|
|
"2. ఎక్కువ భాగం డేటా లేని పూర్తి వరుసలు లేదా కాలమ్స్ తొలగించవచ్చు. \n",
|
|
"3. `DataFrame.dropna(axis=)` పద్ధతి నల్ విలువలను తొలగించడంలో సహాయపడుతుంది. `axis` ఆర్గ్యుమెంట్ వరుసలు తొలగించాలా లేక కాలమ్స్ తొలగించాలా అనేదాన్ని సూచిస్తుంది. \n",
|
|
"4. `how` ఆర్గ్యుమెంట్ కూడా ఉపయోగించవచ్చు. డిఫాల్ట్గా ఇది `any` గా సెట్ ఉంటుంది. కాబట్టి, ఇది ఏదైనా నల్ విలువ ఉన్న వరుసలు/కాలమ్స్ మాత్రమే తొలగిస్తుంది. అన్ని విలువలు నల్ ఉన్న వరుసలు/కాలమ్స్ మాత్రమే తొలగించాలంటే దీన్ని `all` గా సెట్ చేయవచ్చు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "oXXSfQFHgRsF"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "ExUwQRxpgRsF",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# How might you go about dropping just column 3?\n",
|
|
"# Hint: remember that you will need to supply both the axis parameter and the how parameter.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "38kwAihWgRsG"
|
|
},
|
|
"source": [
|
|
"`thresh` పారామీటర్ మీకు మరింత సూక్ష్మ నియంత్రణను ఇస్తుంది: మీరు ఒక వరుస లేదా కాలమ్ నిల్వ చేయబడేందుకు అవసరమైన *నాన్-నల్* విలువల సంఖ్యను సెట్ చేస్తారు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 80
|
|
},
|
|
"id": "M9dCNMaagRsG",
|
|
"outputId": "8093713a-54d2-4e54-c73f-4eea315cb6f2",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" <th>3</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2 3\n",
|
|
"1 2.0 5.0 8 NaN"
|
|
]
|
|
},
|
|
"execution_count": 27,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4.dropna(axis='rows', thresh=3)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "fmSFnzZegRsG"
|
|
},
|
|
"source": [
|
|
"ఇక్కడ, మొదటి మరియు చివరి వరుసలను తొలగించారు, ఎందుకంటే అవి కేవలం రెండు నాన్-నల్ విలువలను మాత్రమే కలిగి ఉన్నాయి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "mCcxLGyUgRsG"
|
|
},
|
|
"source": [
|
|
"### నల్ విలువలను నింపడం\n",
|
|
"\n",
|
|
"కొన్నిసార్లు సరైనవిగా ఉండగల విలువలతో మిస్సింగ్ విలువలను నింపడం అర్థం కలిగిస్తుంది. నల్ విలువలను నింపడానికి కొన్ని సాంకేతికతలు ఉన్నాయి. మొదటిది డొమైన్ జ్ఞానం (డేటాసెట్ ఆధారపడిన విషయం పై జ్ఞానం) ఉపయోగించి మిస్సింగ్ విలువలను ఏదో విధంగా అంచనా వేయడం.\n",
|
|
"\n",
|
|
"మీరు దీన్ని స్థానంలోనే చేయడానికి `isnull` ఉపయోగించవచ్చు, కానీ మీరు నింపవలసిన విలువలు ఎక్కువగా ఉంటే అది కష్టంగా ఉండవచ్చు. డేటా సైన్స్లో ఇది చాలా సాధారణ పని కావడంతో, pandas `fillna`ని అందిస్తుంది, ఇది మిస్సింగ్ విలువలను మీరు ఎంచుకున్న విలువతో మార్చిన `Series` లేదా `DataFrame` యొక్క కాపీని ఇస్తుంది. ఇది ప్రాక్టీస్లో ఎలా పనిచేస్తుందో చూడటానికి మరొక ఉదాహరణ `Series`ని సృష్టిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "CE8S7louLezV"
|
|
},
|
|
"source": [
|
|
"### వర్గీకృత డేటా(సంఖ్యాత్మకంగా లేని)\n",
|
|
"ముందుగా సంఖ్యాత్మకంగా లేని డేటాను పరిశీలిద్దాం. డేటాసెట్లలో, మనకు వర్గీకృత డేటా ఉన్న కాలమ్స్ ఉంటాయి. ఉదా. లింగం, నిజం లేదా తప్పు మొదలైనవి.\n",
|
|
"\n",
|
|
"ఈ కేసులలో ఎక్కువగా, మిస్సింగ్ విలువలను ఆ కాలమ్ యొక్క `mode` తో భర్తీ చేస్తాము. ఉదాహరణకు, మనకు 100 డేటా పాయింట్లు ఉన్నాయని అనుకుందాం, అందులో 90 మంది నిజం అన్నారు, 8 మంది తప్పు అన్నారు మరియు 2 మంది ఎవరూ పూరించలేదు. అప్పుడు, మొత్తం కాలమ్ను పరిగణనలోకి తీసుకుని ఆ 2ని నిజం తో భర్తీ చేయవచ్చు.\n",
|
|
"\n",
|
|
"మళ్లీ, ఇక్కడ డొమైన్ జ్ఞానం ఉపయోగించవచ్చు. మోడ్ తో భర్తీ చేసే ఉదాహరణను పరిశీలిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "MY5faq4yLdpQ",
|
|
"outputId": "19ab472e-1eed-4de8-f8a7-db2a3af3cb1a"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1</td>\n",
|
|
" <td>2</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>3</td>\n",
|
|
" <td>4</td>\n",
|
|
" <td>None</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>5</td>\n",
|
|
" <td>6</td>\n",
|
|
" <td>False</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>7</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>9</td>\n",
|
|
" <td>10</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 1 2 True\n",
|
|
"1 3 4 None\n",
|
|
"2 5 6 False\n",
|
|
"3 7 8 True\n",
|
|
"4 9 10 True"
|
|
]
|
|
},
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_mode = pd.DataFrame([[1,2,\"True\"],\n",
|
|
" [3,4,None],\n",
|
|
" [5,6,\"False\"],\n",
|
|
" [7,8,\"True\"],\n",
|
|
" [9,10,\"True\"]])\n",
|
|
"\n",
|
|
"fill_with_mode"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "MLAoMQOfNPlA"
|
|
},
|
|
"source": [
|
|
"ఇప్పుడు, ముందుగా మోడ్ను కనుగొనుకుందాం, ఆపై `None` విలువను మోడ్తో భర్తీ చేద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "WKy-9Y2tN5jv",
|
|
"outputId": "8da9fa16-e08c-447e-dea1-d4b1db2feebf"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"True 3\n",
|
|
"False 1\n",
|
|
"Name: 2, dtype: int64"
|
|
]
|
|
},
|
|
"execution_count": 29,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_mode[2].value_counts()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "6iNz_zG_OKrx"
|
|
},
|
|
"source": [
|
|
"కాబట్టి, మనం None ను True తో మార్చబోతున్నాము.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"metadata": {
|
|
"id": "TxPKteRvNPOs"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"fill_with_mode[2].fillna('True',inplace=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 31,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "tvas7c9_OPWE",
|
|
"outputId": "ec3c8e44-d644-475e-9e22-c65101965850"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1</td>\n",
|
|
" <td>2</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>3</td>\n",
|
|
" <td>4</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>5</td>\n",
|
|
" <td>6</td>\n",
|
|
" <td>False</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>7</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>9</td>\n",
|
|
" <td>10</td>\n",
|
|
" <td>True</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 1 2 True\n",
|
|
"1 3 4 True\n",
|
|
"2 5 6 False\n",
|
|
"3 7 8 True\n",
|
|
"4 9 10 True"
|
|
]
|
|
},
|
|
"execution_count": 31,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_mode"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "SktitLxxOR16"
|
|
},
|
|
"source": [
|
|
"మనము చూడగలిగినట్లుగా, నల్ విలువ మార్చబడింది. చెప్పాల్సిన అవసరం లేదు, మేము ఎక్కడైనా `'True'` స్థానంలో ఏదైనా రాయవచ్చు మరియు అది మార్చబడేది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "heYe1I0dOmQ_"
|
|
},
|
|
"source": [
|
|
"### సంఖ్యా డేటా\n",
|
|
"ఇప్పుడు, సంఖ్యా డేటాకు వస్తే. ఇక్కడ, మిస్ అయిన విలువలను భర్తీ చేయడానికి రెండు సాధారణ మార్గాలు ఉన్నాయి:\n",
|
|
"\n",
|
|
"1. వరుస యొక్క మాధ్యమంతో భర్తీ చేయండి\n",
|
|
"2. వరుస యొక్క సగటుతో భర్తీ చేయండి\n",
|
|
"\n",
|
|
"మేము మాధ్యమంతో భర్తీ చేస్తాము, అవుట్లయర్లతో కూడిన వక్రీకృత డేటా సందర్భంలో. ఇది ఎందుకంటే మాధ్యమం అవుట్లయర్లకు మన్నించగలదు.\n",
|
|
"\n",
|
|
"డేటా సాధారణీకరించబడినప్పుడు, మేము సగటును ఉపయోగించవచ్చు, ఎందుకంటే ఆ సందర్భంలో, సగటు మరియు మాధ్యమం చాలా దగ్గరగా ఉంటాయి.\n",
|
|
"\n",
|
|
"మొదట, మనం సాధారణంగా పంపిణీ చేయబడిన ఒక కాలమ్ తీసుకుందాం మరియు ఆ కాలమ్ యొక్క సగటుతో మిస్ అయిన విలువను నింపుదాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 32,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "09HM_2feOj5Y",
|
|
"outputId": "7e309013-9acb-411c-9b06-4de795bbeeff"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>-2.0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>-1.0</td>\n",
|
|
" <td>2</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>4</td>\n",
|
|
" <td>5</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>6</td>\n",
|
|
" <td>7</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>9</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 -2.0 0 1\n",
|
|
"1 -1.0 2 3\n",
|
|
"2 NaN 4 5\n",
|
|
"3 1.0 6 7\n",
|
|
"4 2.0 8 9"
|
|
]
|
|
},
|
|
"execution_count": 32,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_mean = pd.DataFrame([[-2,0,1],\n",
|
|
" [-1,2,3],\n",
|
|
" [np.nan,4,5],\n",
|
|
" [1,6,7],\n",
|
|
" [2,8,9]])\n",
|
|
"\n",
|
|
"fill_with_mean"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "ka7-wNfzSxbx"
|
|
},
|
|
"source": [
|
|
"కాలమ్ యొక్క సగటు విలువ ఏమిటి\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 33,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "XYtYEf5BSxFL",
|
|
"outputId": "68a78d18-f0e5-4a9a-a959-2c3676a57c70"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"0.0"
|
|
]
|
|
},
|
|
"execution_count": 33,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"np.mean(fill_with_mean[0])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "oBSRGxKRS39K"
|
|
},
|
|
"source": [
|
|
"సగటుతో నింపడం\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 34,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "FzncQLmuS5jh",
|
|
"outputId": "00f74fff-01f4-4024-c261-796f50f01d2e"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>-2.0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>-1.0</td>\n",
|
|
" <td>2</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>0.0</td>\n",
|
|
" <td>4</td>\n",
|
|
" <td>5</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>6</td>\n",
|
|
" <td>7</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>9</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 -2.0 0 1\n",
|
|
"1 -1.0 2 3\n",
|
|
"2 0.0 4 5\n",
|
|
"3 1.0 6 7\n",
|
|
"4 2.0 8 9"
|
|
]
|
|
},
|
|
"execution_count": 34,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_mean[0].fillna(np.mean(fill_with_mean[0]),inplace=True)\n",
|
|
"fill_with_mean"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "CwpVFCrPTC5z"
|
|
},
|
|
"source": [
|
|
"మనము చూడగలిగినట్లుగా, లేని విలువ దాని సగటుతో మార్చబడింది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "jIvF13a1i00Z"
|
|
},
|
|
"source": [
|
|
"ఇప్పుడు మనం మరో డేటాఫ్రేమ్ను ప్రయత్నిద్దాం, ఈ సారి None విలువలను కాలమ్ యొక్క మాధ్యమంతో మార్చబోతున్నాము.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 35,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "DA59Bqo3jBYZ",
|
|
"outputId": "85dae6ec-7394-4c36-fda0-e04769ec4a32"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>-2</td>\n",
|
|
" <td>0.0</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>-1</td>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>0</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>5</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>1</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>7</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>2</td>\n",
|
|
" <td>8.0</td>\n",
|
|
" <td>9</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 -2 0.0 1\n",
|
|
"1 -1 2.0 3\n",
|
|
"2 0 NaN 5\n",
|
|
"3 1 6.0 7\n",
|
|
"4 2 8.0 9"
|
|
]
|
|
},
|
|
"execution_count": 35,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_median = pd.DataFrame([[-2,0,1],\n",
|
|
" [-1,2,3],\n",
|
|
" [0,np.nan,5],\n",
|
|
" [1,6,7],\n",
|
|
" [2,8,9]])\n",
|
|
"\n",
|
|
"fill_with_median"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "mM1GpXYmjHnc"
|
|
},
|
|
"source": [
|
|
"రెండవ కాలమ్ యొక్క మధ్య విలువ ఏమిటి\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 36,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "uiDy5v3xjHHX",
|
|
"outputId": "564b6b74-2004-4486-90d4-b39330a64b88"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"4.0"
|
|
]
|
|
},
|
|
"execution_count": 36,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_median[1].median()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "z9PLF75Jj_1s"
|
|
},
|
|
"source": [
|
|
"మధ్యమంతో నింపడం\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 37,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "lFKbOxCMkBbg",
|
|
"outputId": "a8bd18fb-2765-47d4-e5fe-e965f57ed1f4"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>-2</td>\n",
|
|
" <td>0.0</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>-1</td>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>0</td>\n",
|
|
" <td>4.0</td>\n",
|
|
" <td>5</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>1</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>7</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>2</td>\n",
|
|
" <td>8.0</td>\n",
|
|
" <td>9</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2\n",
|
|
"0 -2 0.0 1\n",
|
|
"1 -1 2.0 3\n",
|
|
"2 0 4.0 5\n",
|
|
"3 1 6.0 7\n",
|
|
"4 2 8.0 9"
|
|
]
|
|
},
|
|
"execution_count": 37,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"fill_with_median[1].fillna(fill_with_median[1].median(),inplace=True)\n",
|
|
"fill_with_median"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "8JtQ53GSkKWC"
|
|
},
|
|
"source": [
|
|
"మనం చూడగలిగినట్లుగా, NaN విలువను కాలమ్ యొక్క మధ్యమ విలువతో మార్చారు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 38,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "0ybtWLDdgRsG",
|
|
"outputId": "b8c238ef-6024-4ee2-be2b-aa1f0fcac61d",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"a 1.0\n",
|
|
"b NaN\n",
|
|
"c 2.0\n",
|
|
"d NaN\n",
|
|
"e 3.0\n",
|
|
"dtype: float64"
|
|
]
|
|
},
|
|
"execution_count": 38,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example5 = pd.Series([1, np.nan, 2, None, 3], index=list('abcde'))\n",
|
|
"example5"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "yrsigxRggRsH"
|
|
},
|
|
"source": [
|
|
"మీరు అన్ని నల్ ఎంట్రీలను ఒకే విలువతో పూరించవచ్చు, ఉదాహరణకు `0`:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 39,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "KXMIPsQdgRsH",
|
|
"outputId": "aeedfa0a-a421-4c2f-cb0d-183ce8f0c91d",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"a 1.0\n",
|
|
"b 0.0\n",
|
|
"c 2.0\n",
|
|
"d 0.0\n",
|
|
"e 3.0\n",
|
|
"dtype: float64"
|
|
]
|
|
},
|
|
"execution_count": 39,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example5.fillna(0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "RRlI5f_hkfKe"
|
|
},
|
|
"source": [
|
|
"> ముఖ్యమైన విషయాలు:\n",
|
|
"1. డేటా తక్కువగా ఉన్నప్పుడు లేదా లేని డేటాను పూరించడానికి ఒక వ్యూహం ఉన్నప్పుడు మాత్రమే లేని విలువలను పూరించాలి.\n",
|
|
"2. లేని విలువలను సుమారు అంచనా వేయడానికి డొమైన్ జ్ఞానం ఉపయోగించవచ్చు.\n",
|
|
"3. వర్గీకరణ డేటా కోసం, ఎక్కువగా లేని విలువలను ఆ కాలమ్ యొక్క మోడ్తో భర్తీ చేస్తారు.\n",
|
|
"4. సంఖ్యాత్మక డేటా కోసం, లేని విలువలను సాధారణంగా సగటు (సాధారణీకరించిన డేటాసెట్ల కోసం) లేదా కాలమ్ల మధ్య విలువతో పూరిస్తారు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "FI9MmqFJgRsH"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 40,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "af-ezpXdgRsH",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# What happens if you try to fill null values with a string, like ''?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "kq3hw1kLgRsI"
|
|
},
|
|
"source": [
|
|
"మీరు **ఫార్వర్డ్-ఫిల్** చేయవచ్చు, అంటే చివరి చెల్లుబాటు అయ్యే విలువను ఉపయోగించి నల్ విలువను పూరించవచ్చు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 41,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "vO3BuNrggRsI",
|
|
"outputId": "e2bc591b-0b48-4e88-ee65-754f2737c196",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"a 1.0\n",
|
|
"b 1.0\n",
|
|
"c 2.0\n",
|
|
"d 2.0\n",
|
|
"e 3.0\n",
|
|
"dtype: float64"
|
|
]
|
|
},
|
|
"execution_count": 41,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example5.fillna(method='ffill')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "nDXeYuHzgRsI"
|
|
},
|
|
"source": [
|
|
"మీరు కూడా **back-fill** చేయవచ్చు తద్వారా తదుపరి చెల్లుబాటు అయ్యే విలువను వెనుకకు పంపించి null ని నింపవచ్చు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 42,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "4M5onHcEgRsI",
|
|
"outputId": "8f32b185-40dd-4a9f-bd85-54d6b6a414fe",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"a 1.0\n",
|
|
"b 2.0\n",
|
|
"c 2.0\n",
|
|
"d 3.0\n",
|
|
"e 3.0\n",
|
|
"dtype: float64"
|
|
]
|
|
},
|
|
"execution_count": 42,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example5.fillna(method='bfill')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "MbBzTom5gRsI"
|
|
},
|
|
"source": [
|
|
"మీరు ఊహించగలిగినట్లుగా, ఇది DataFrames తో కూడా అదే విధంగా పనిచేస్తుంది, కానీ మీరు శూన్య విలువలను నింపడానికి ఒక `axis` ను కూడా నిర్దేశించవచ్చు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 43,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "aRpIvo4ZgRsI",
|
|
"outputId": "905a980a-a808-4eca-d0ba-224bd7d85955",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" <th>3</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>7</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>9</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2 3\n",
|
|
"0 1.0 NaN 7 NaN\n",
|
|
"1 2.0 5.0 8 NaN\n",
|
|
"2 NaN 6.0 9 NaN"
|
|
]
|
|
},
|
|
"execution_count": 43,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 44,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "VM1qtACAgRsI",
|
|
"outputId": "71f2ad28-9b4e-4ff4-f5c3-e731eb489ade",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" <th>3</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>7.0</td>\n",
|
|
" <td>7.0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8.0</td>\n",
|
|
" <td>8.0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>9.0</td>\n",
|
|
" <td>9.0</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2 3\n",
|
|
"0 1.0 1.0 7.0 7.0\n",
|
|
"1 2.0 5.0 8.0 8.0\n",
|
|
"2 NaN 6.0 9.0 9.0"
|
|
]
|
|
},
|
|
"execution_count": 44,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4.fillna(method='ffill', axis=1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "ZeMc-I1EgRsI"
|
|
},
|
|
"source": [
|
|
"గమనించండి, ముందుకు నింపడానికి ముందు విలువ అందుబాటులో లేకపోతే, శూన్య విలువ అలాగే ఉంటుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "eeAoOU0RgRsJ"
|
|
},
|
|
"source": [
|
|
"### వ్యాయామం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 45,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "e8S-CjW8gRsJ",
|
|
"trusted": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# What output does example4.fillna(method='bfill', axis=1) produce?\n",
|
|
"# What about example4.fillna(method='ffill') or example4.fillna(method='bfill')?\n",
|
|
"# Can you think of a longer code snippet to write that can fill all of the null values in example4?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "YHgy0lIrgRsJ"
|
|
},
|
|
"source": [
|
|
"మీరు `fillna` ను ఉపయోగించే విధానంలో సృజనాత్మకంగా ఉండవచ్చు. ఉదాహరణకు, మళ్లీ `example4` ను చూద్దాం, కానీ ఈసారి మిస్సింగ్ విలువలను `DataFrame` లోని అన్ని విలువల సగటుతో నింపుదాం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 46,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "OtYVErEygRsJ",
|
|
"outputId": "708b1e67-45ca-44bf-a5ee-8b2de09ece73",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>0</th>\n",
|
|
" <th>1</th>\n",
|
|
" <th>2</th>\n",
|
|
" <th>3</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>1.0</td>\n",
|
|
" <td>5.5</td>\n",
|
|
" <td>7</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>2.0</td>\n",
|
|
" <td>5.0</td>\n",
|
|
" <td>8</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>1.5</td>\n",
|
|
" <td>6.0</td>\n",
|
|
" <td>9</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0 1 2 3\n",
|
|
"0 1.0 5.5 7 NaN\n",
|
|
"1 2.0 5.0 8 NaN\n",
|
|
"2 1.5 6.0 9 NaN"
|
|
]
|
|
},
|
|
"execution_count": 46,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example4.fillna(example4.mean())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "zpMvCkLSgRsJ"
|
|
},
|
|
"source": [
|
|
"కాలమ్ 3 ఇంకా విలువలేని స్థితిలో ఉందని గమనించండి: డిఫాల్ట్ దిశ విలువలను వరుసల వారీగా నింపడమే.\n",
|
|
"\n",
|
|
"> **గుర్తుంచుకోవాల్సిన విషయం:** మీ డేటాసెట్లలో లేని విలువలతో వ్యవహరించడానికి అనేక మార్గాలు ఉన్నాయి. మీరు ఉపయోగించే నిర్దిష్ట వ్యూహం (వాటిని తొలగించడం, మార్చడం, లేదా మీరు ఎలా మార్చాలో) ఆ డేటా యొక్క ప్రత్యేకతల ఆధారంగా ఉండాలి. మీరు డేటాసెట్లను ఎక్కువగా నిర్వహించగలిగే కొద్దీ లేని విలువలతో ఎలా వ్యవహరించాలో మెరుగైన అవగాహన ఏర్పడుతుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "bauDnESIl9FH"
|
|
},
|
|
"source": [
|
|
"### వర్గీకృత డేటాను ఎన్కోడ్ చేయడం\n",
|
|
"\n",
|
|
"యంత్ర అభ్యాస నమూనాలు సంఖ్యలతో మరియు ఏదైనా రకమైన సంఖ్యా డేటాతో మాత్రమే వ్యవహరిస్తాయి. అవి అవును మరియు కాదు మధ్య తేడాను చెప్పలేవు, కానీ 0 మరియు 1 మధ్య తేడాను గుర్తించగలవు. కాబట్టి, మిస్సింగ్ విలువలను పూరించిన తర్వాత, నమూనా అర్థం చేసుకునేందుకు వర్గీకృత డేటాను కొన్ని సంఖ్యా రూపంలో ఎన్కోడ్ చేయాలి.\n",
|
|
"\n",
|
|
"ఎన్కోడ్ చేయడం రెండు విధాలుగా చేయవచ్చు. మేము వాటిని తరువాత చర్చించబోతున్నాము.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "uDq9SxB7mu5i"
|
|
},
|
|
"source": [
|
|
"**లేబుల్ ఎన్కోడింగ్**\n",
|
|
"\n",
|
|
"\n",
|
|
"లేబుల్ ఎన్కోడింగ్ అనేది ప్రాథమికంగా ప్రతి వర్గాన్ని ఒక సంఖ్యగా మార్చడం. ఉదాహరణకు, మనకు విమాన ప్రయాణికుల డేటాసెట్ ఉందని మరియు వారి తరగతి ఉన్న ఒక కాలమ్ ['బిజినెస్ క్లాస్', 'ఎకానమీ క్లాస్', 'ఫస్ట్ క్లాస్'] అని తీసుకుందాం. ఈ లేబుల్ ఎన్కోడింగ్ చేస్తే, ఇది [0,1,2] గా మారుతుంది. కోడ్ ద్వారా ఒక ఉదాహరణ చూద్దాం. మనం రాబోయే నోట్బుక్స్లో `scikit-learn` నేర్చుకుంటున్నందున, ఇక్కడ దాన్ని ఉపయోగించము.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 47,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 235
|
|
},
|
|
"id": "1vGz7uZyoWHL",
|
|
"outputId": "9e252855-d193-4103-a54d-028ea7787b34"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>ID</th>\n",
|
|
" <th>class</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>10</td>\n",
|
|
" <td>business class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>20</td>\n",
|
|
" <td>first class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>30</td>\n",
|
|
" <td>economy class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>40</td>\n",
|
|
" <td>economy class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>50</td>\n",
|
|
" <td>economy class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>5</th>\n",
|
|
" <td>60</td>\n",
|
|
" <td>business class</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" ID class\n",
|
|
"0 10 business class\n",
|
|
"1 20 first class\n",
|
|
"2 30 economy class\n",
|
|
"3 40 economy class\n",
|
|
"4 50 economy class\n",
|
|
"5 60 business class"
|
|
]
|
|
},
|
|
"execution_count": 47,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"label = pd.DataFrame([\n",
|
|
" [10,'business class'],\n",
|
|
" [20,'first class'],\n",
|
|
" [30, 'economy class'],\n",
|
|
" [40, 'economy class'],\n",
|
|
" [50, 'economy class'],\n",
|
|
" [60, 'business class']\n",
|
|
"],columns=['ID','class'])\n",
|
|
"label"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "IDHnkwTYov-h"
|
|
},
|
|
"source": [
|
|
"మొదటి కాలమ్పై లేబుల్ ఎంకోడింగ్ చేయడానికి, మేము ప్రతి తరగతిని ఒక సంఖ్యకు మ్యాపింగ్ చేయడం మొదట చేయాలి, ఆ తర్వాత మార్చాలి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 48,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 235
|
|
},
|
|
"id": "ZC5URJG3o1ES",
|
|
"outputId": "aab0f1e7-e0f3-4c14-8459-9f9168c85437"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>ID</th>\n",
|
|
" <th>class</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>10</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>20</td>\n",
|
|
" <td>2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>30</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>40</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>50</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>5</th>\n",
|
|
" <td>60</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" ID class\n",
|
|
"0 10 0\n",
|
|
"1 20 2\n",
|
|
"2 30 1\n",
|
|
"3 40 1\n",
|
|
"4 50 1\n",
|
|
"5 60 0"
|
|
]
|
|
},
|
|
"execution_count": 48,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"class_labels = {'business class':0,'economy class':1,'first class':2}\n",
|
|
"label['class'] = label['class'].replace(class_labels)\n",
|
|
"label"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "ftnF-TyapOPt"
|
|
},
|
|
"source": [
|
|
"మనం చూడగలిగినట్లుగా, అవుట్పుట్ మనం ఊహించినదానికి సరిపోతుంది. కాబట్టి, లేబుల్ ఎంకోడింగ్ ఎప్పుడు ఉపయోగిస్తాము? లేబుల్ ఎంకోడింగ్ క్రింది రెండు సందర్భాలలో ఒకటి లేదా రెండింటిలో ఉపయోగిస్తారు:\n",
|
|
"1. వర్గాల సంఖ్య ఎక్కువగా ఉన్నప్పుడు\n",
|
|
"2. వర్గాలు క్రమంలో ఉన్నప్పుడు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "eQPAPVwsqWT7"
|
|
},
|
|
"source": [
|
|
"**ఒన్ హాట్ ఎన్కోడింగ్**\n",
|
|
"\n",
|
|
"మరొక రకమైన ఎన్కోడింగ్ ఒన్ హాట్ ఎన్కోడింగ్. ఈ రకమైన ఎన్కోడింగ్లో, కాలమ్ యొక్క ప్రతి వర్గం వేరే కాలమ్గా జోడించబడుతుంది మరియు ప్రతి డేటాపాయింట్ ఆ వర్గాన్ని కలిగి ఉన్నదో లేదో ఆధారంగా 0 లేదా 1 పొందుతుంది. కాబట్టి, n వేర్వేరు వర్గాలు ఉంటే, n కాలమ్స్ డేటాఫ్రేమ్కు జోడించబడతాయి.\n",
|
|
"\n",
|
|
"ఉదాహరణకు, అదే విమాన తరగతి ఉదాహరణ తీసుకుందాం. వర్గాలు: ['బిజినెస్ క్లాస్', 'ఎకానమీ క్లాస్', 'ఫస్ట్ క్లాస్'] . కాబట్టి, ఒన్ హాట్ ఎన్కోడింగ్ చేస్తే, datasetకి క్రింది మూడు కాలమ్స్ జోడించబడతాయి: ['class_business class','class_economy class','class_first class'].\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 49,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 235
|
|
},
|
|
"id": "ZM0eVh0ArKUL",
|
|
"outputId": "83238a76-b3a5-418d-c0b6-605b02b6891b"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>ID</th>\n",
|
|
" <th>class</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>10</td>\n",
|
|
" <td>business class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>20</td>\n",
|
|
" <td>first class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>30</td>\n",
|
|
" <td>economy class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>40</td>\n",
|
|
" <td>economy class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>50</td>\n",
|
|
" <td>economy class</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>5</th>\n",
|
|
" <td>60</td>\n",
|
|
" <td>business class</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" ID class\n",
|
|
"0 10 business class\n",
|
|
"1 20 first class\n",
|
|
"2 30 economy class\n",
|
|
"3 40 economy class\n",
|
|
"4 50 economy class\n",
|
|
"5 60 business class"
|
|
]
|
|
},
|
|
"execution_count": 49,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"one_hot = pd.DataFrame([\n",
|
|
" [10,'business class'],\n",
|
|
" [20,'first class'],\n",
|
|
" [30, 'economy class'],\n",
|
|
" [40, 'economy class'],\n",
|
|
" [50, 'economy class'],\n",
|
|
" [60, 'business class']\n",
|
|
"],columns=['ID','class'])\n",
|
|
"one_hot"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "aVnZ7paDrWmb"
|
|
},
|
|
"source": [
|
|
"మనం మొదటి కాలమ్పై వన్ హాట్ ఎంకోడింగ్ చేయండి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 50,
|
|
"metadata": {
|
|
"id": "RUPxf7egrYKr"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"one_hot_data = pd.get_dummies(one_hot,columns=['class'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 51,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 235
|
|
},
|
|
"id": "TM37pHsFr4ge",
|
|
"outputId": "7be15f53-79b2-447a-979c-822658339a9e"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>ID</th>\n",
|
|
" <th>class_business class</th>\n",
|
|
" <th>class_economy class</th>\n",
|
|
" <th>class_first class</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>10</td>\n",
|
|
" <td>1</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>20</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>30</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>1</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>1</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>50</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>1</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>5</th>\n",
|
|
" <td>60</td>\n",
|
|
" <td>1</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" ID class_business class class_economy class class_first class\n",
|
|
"0 10 1 0 0\n",
|
|
"1 20 0 0 1\n",
|
|
"2 30 0 1 0\n",
|
|
"3 40 0 1 0\n",
|
|
"4 50 0 1 0\n",
|
|
"5 60 1 0 0"
|
|
]
|
|
},
|
|
"execution_count": 51,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"one_hot_data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "_zXRLOjXujdA"
|
|
},
|
|
"source": [
|
|
"ప్రతి ఒక హాట్ ఎన్కోడ్ చేసిన కాలమ్ 0 లేదా 1 ను కలిగి ఉంటుంది, ఇది ఆ డేటాపాయింట్ కోసం ఆ వర్గం ఉందో లేదో సూచిస్తుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "bDnC4NQOu0qr"
|
|
},
|
|
"source": [
|
|
"మనం ఎప్పుడు వన్ హాట్ ఎంకోడింగ్ ఉపయోగిస్తాము? వన్ హాట్ ఎంకోడింగ్ క్రింది రెండు సందర్భాలలో ఒకటి లేదా రెండింటిలో ఉపయోగిస్తారు:\n",
|
|
"\n",
|
|
"1. వర్గాల సంఖ్య మరియు డేటాసెట్ పరిమాణం తక్కువగా ఉన్నప్పుడు.\n",
|
|
"2. వర్గాలు ఎటువంటి ప్రత్యేక క్రమాన్ని అనుసరించకపోతే.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "XnUmci_4uvyu"
|
|
},
|
|
"source": [
|
|
"> ముఖ్యాంశాలు:\n",
|
|
"1. ఎన్కోడింగ్ అనేది సంఖ్యలేని డేటాను సంఖ్యా డేటాగా మార్చడానికి చేయబడుతుంది.\n",
|
|
"2. ఎన్కోడింగ్ రెండు రకాలుగా ఉంటాయి: లేబుల్ ఎన్కోడింగ్ మరియు వన్ హాట్ ఎన్కోడింగ్, ఇవి డేటాసెట్ అవసరాల ఆధారంగా చేయబడవచ్చు.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "K8UXOJYRgRsJ"
|
|
},
|
|
"source": [
|
|
"## డూప్లికేట్ డేటాను తొలగించడం\n",
|
|
"\n",
|
|
"> **అధ్యయన లక్ష్యం:** ఈ ఉపవిభాగం ముగిసే వరకు, మీరు DataFrames నుండి డూప్లికేట్ విలువలను గుర్తించి తొలగించడంలో సౌకర్యంగా ఉండాలి.\n",
|
|
"\n",
|
|
"లేకపోయిన డేటా తో పాటు, మీరు నిజజీవిత డేటాసెట్లలో తరచుగా డూప్లికేట్ డేటాను ఎదుర్కొంటారు. అదృష్టవశాత్తు, pandas డూప్లికేట్ ఎంట్రీలను గుర్తించడం మరియు తొలగించడం కోసం సులభమైన మార్గాన్ని అందిస్తుంది.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "qrEG-Wa0gRsJ"
|
|
},
|
|
"source": [
|
|
"### డూప్లికేట్లను గుర్తించడం: `duplicated`\n",
|
|
"\n",
|
|
"మీరు pandas లోని `duplicated` పద్ధతిని ఉపయోగించి డూప్లికేట్ విలువలను సులభంగా గుర్తించవచ్చు, ఇది ఒక `DataFrame` లోని ఎంట్రీ ఒక ముందటి ఎంట్రీ యొక్క డూప్లికేట్ కాదా అనే విషయాన్ని సూచించే బూలియన్ మాస్క్ ను తిరిగి ఇస్తుంది. దీన్ని ప్రదర్శించడానికి మరొక ఉదాహరణ `DataFrame` ను సృష్టిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 52,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"id": "ZLu6FEnZgRsJ",
|
|
"outputId": "376512d1-d842-4db1-aea3-71052aeeecaf",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>letters</th>\n",
|
|
" <th>numbers</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>A</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>B</td>\n",
|
|
" <td>2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>A</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>B</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>B</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" letters numbers\n",
|
|
"0 A 1\n",
|
|
"1 B 2\n",
|
|
"2 A 1\n",
|
|
"3 B 3\n",
|
|
"4 B 3"
|
|
]
|
|
},
|
|
"execution_count": 52,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example6 = pd.DataFrame({'letters': ['A','B'] * 2 + ['B'],\n",
|
|
" 'numbers': [1, 2, 1, 3, 3]})\n",
|
|
"example6"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 53,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "cIduB5oBgRsK",
|
|
"outputId": "3da27b3d-4d69-4e1d-bb52-0af21bae87f2",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"0 False\n",
|
|
"1 False\n",
|
|
"2 True\n",
|
|
"3 False\n",
|
|
"4 True\n",
|
|
"dtype: bool"
|
|
]
|
|
},
|
|
"execution_count": 53,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example6.duplicated()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "0eDRJD4SgRsK"
|
|
},
|
|
"source": [
|
|
"### డూప్లికేట్లను తొలగించడం: `drop_duplicates`\n",
|
|
"`drop_duplicates` సాదారణంగా `duplicated` విలువలు అన్నీ `False` అయిన డేటా యొక్క కాపీని తిరిగి ఇస్తుంది:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 54,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 142
|
|
},
|
|
"id": "w_YPpqIqgRsK",
|
|
"outputId": "ac66bd2f-8671-4744-87f5-8b8d96553dea",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>letters</th>\n",
|
|
" <th>numbers</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>A</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>B</td>\n",
|
|
" <td>2</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>B</td>\n",
|
|
" <td>3</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" letters numbers\n",
|
|
"0 A 1\n",
|
|
"1 B 2\n",
|
|
"3 B 3"
|
|
]
|
|
},
|
|
"execution_count": 54,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example6.drop_duplicates()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "69AqoCZAgRsK"
|
|
},
|
|
"source": [
|
|
"`duplicated` మరియు `drop_duplicates` రెండూ డిఫాల్ట్గా అన్ని కాలమ్స్ను పరిగణలోకి తీసుకుంటాయి కానీ మీరు మీ `DataFrame`లోని కొన్ని కాలమ్స్ మాత్రమే పరిశీలించమని నిర్దేశించవచ్చు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 55,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 111
|
|
},
|
|
"id": "BILjDs67gRsK",
|
|
"outputId": "ef6dcc08-db8b-4352-c44e-5aa9e2bec0d3",
|
|
"trusted": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>letters</th>\n",
|
|
" <th>numbers</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>A</td>\n",
|
|
" <td>1</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>B</td>\n",
|
|
" <td>2</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" letters numbers\n",
|
|
"0 A 1\n",
|
|
"1 B 2"
|
|
]
|
|
},
|
|
"execution_count": 55,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"example6.drop_duplicates(['letters'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "GvX4og1EgRsL"
|
|
},
|
|
"source": [
|
|
"> **ముఖ్యాంశం:** డూప్లికేట్ డేటాను తొలగించడం ప్రతి డేటా-సైన్స్ ప్రాజెక్ట్లో ఒక ముఖ్యమైన భాగం. డూప్లికేట్ డేటా మీ విశ్లేషణల ఫలితాలను మార్చి, మీకు తప్పు ఫలితాలను ఇవ్వవచ్చు!\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## వాస్తవ ప్రపంచ డేటా నాణ్యత తనిఖీలు\n",
|
|
"\n",
|
|
"> **అధ్యయన లక్ష్యం:** ఈ విభాగం ముగింపులో, మీరు అసమానమైన వర్గీకరణ విలువలు, అసాధారణ సంఖ్యా విలువలు (అత్యధికాలు), మరియు వేరియేషన్లతో కూడిన డూప్లికేట్ ఎంటిటీలను గుర్తించి సరిచేయడంలో సౌకర్యంగా ఉండాలి.\n",
|
|
"\n",
|
|
"తప్పిపోయిన విలువలు మరియు ఖచ్చితమైన డూప్లికేట్లు సాధారణ సమస్యలు అయినప్పటికీ, వాస్తవ ప్రపంచ డేటాసెట్లు తరచుగా మరింత సున్నితమైన సమస్యలను కలిగి ఉంటాయి:\n",
|
|
"\n",
|
|
"1. **అసమానమైన వర్గీకరణ విలువలు**: అదే వర్గం వేరుగా వ్రాయబడటం (ఉదా: \"USA\", \"U.S.A\", \"United States\")\n",
|
|
"2. **అసాధారణ సంఖ్యా విలువలు**: డేటా ఎంట్రీ లోపాలను సూచించే అత్యధిక అత్యధికాలు (ఉదా: వయస్సు = 999)\n",
|
|
"3. **సమీప డూప్లికేట్ వరుసలు**: స్వల్ప వేరియేషన్లతో అదే ఎంటిటీని సూచించే రికార్డులు\n",
|
|
"\n",
|
|
"ఈ సమస్యలను గుర్తించి నిర్వహించడానికి సాంకేతికతలను పరిశీలిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### ఒక నమూనా \"అశుభ్రమైన\" డేటాసెట్ సృష్టించడం\n",
|
|
"\n",
|
|
"మొదట, మనం సాధారణంగా నిజమైన ప్రపంచ డేటాలో ఎదురయ్యే సమస్యల రకాలను కలిగిన ఒక నమూనా డేటాసెట్ను సృష్టిద్దాం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"# Create a sample dataset with quality issues\n",
|
|
"dirty_data = pd.DataFrame({\n",
|
|
" 'customer_id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n",
|
|
" 'name': ['John Smith', 'Jane Doe', 'John Smith', 'Bob Johnson', \n",
|
|
" 'Alice Williams', 'Charlie Brown', 'John Smith', 'Eva Martinez',\n",
|
|
" 'Bob Johnson', 'Diana Prince', 'Frank Castle', 'Alice Williams'],\n",
|
|
" 'age': [25, 32, 25, 45, 28, 199, 25, 31, 45, 27, -5, 28],\n",
|
|
" 'country': ['USA', 'UK', 'U.S.A', 'Canada', 'USA', 'United Kingdom',\n",
|
|
" 'United States', 'Mexico', 'canada', 'USA', 'UK', 'usa'],\n",
|
|
" 'purchase_amount': [100.50, 250.00, 105.00, 320.00, 180.00, 90.00,\n",
|
|
" 102.00, 275.00, 325.00, 195.00, 410.00, 185.00]\n",
|
|
"})\n",
|
|
"\n",
|
|
"print(\"Sample 'Dirty' Dataset:\")\n",
|
|
"print(dirty_data)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 1. అసమంజసమైన వర్గీకరణ విలువలను గుర్తించడం\n",
|
|
"\n",
|
|
"`country` కాలమ్లో ఒకే దేశానికి అనేక ప్రాతినిధ్యాలు ఉన్నాయని గమనించండి. ఈ అసమంజసాలను గుర్తిద్దాం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Check unique values in the country column\n",
|
|
"print(\"Unique country values:\")\n",
|
|
"print(dirty_data['country'].unique())\n",
|
|
"print(f\"\\nTotal unique values: {dirty_data['country'].nunique()}\")\n",
|
|
"\n",
|
|
"# Count occurrences of each variation\n",
|
|
"print(\"\\nValue counts:\")\n",
|
|
"print(dirty_data['country'].value_counts())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### వర్గీకరణ విలువలను ప్రమాణీకరించడం\n",
|
|
"\n",
|
|
"మేము ఈ విలువలను ప్రమాణీకరించడానికి ఒక మ్యాపింగ్ను సృష్టించవచ్చు. ఒక సాదా విధానం అంటే చిన్న అక్షరాలకు మార్చి ఒక మ్యాపింగ్ డిక్షనరీని సృష్టించడం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create a standardization mapping\n",
|
|
"country_mapping = {\n",
|
|
" 'usa': 'USA',\n",
|
|
" 'u.s.a': 'USA',\n",
|
|
" 'united states': 'USA',\n",
|
|
" 'uk': 'UK',\n",
|
|
" 'united kingdom': 'UK',\n",
|
|
" 'canada': 'Canada',\n",
|
|
" 'mexico': 'Mexico'\n",
|
|
"}\n",
|
|
"\n",
|
|
"# Standardize the country column\n",
|
|
"dirty_data['country_clean'] = dirty_data['country'].str.lower().map(country_mapping)\n",
|
|
"\n",
|
|
"print(\"Before standardization:\")\n",
|
|
"print(dirty_data['country'].value_counts())\n",
|
|
"print(\"\\nAfter standardization:\")\n",
|
|
"print(dirty_data[['country_clean']].value_counts())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**వికల్పం: ఫజ్జీ మ్యాచ్ చేయడం ఉపయోగించడం**\n",
|
|
"\n",
|
|
"మరింత క్లిష్టమైన సందర్భాల కోసం, మనం `rapidfuzz` లైబ్రరీతో ఫజ్జీ స్ట్రింగ్ మ్యాచ్ చేయడం ఉపయోగించి సాదృశ్యమైన స్ట్రింగులను ఆటోమేటిక్గా గుర్తించవచ్చు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"try:\n",
|
|
" from rapidfuzz import process, fuzz\n",
|
|
"except ImportError:\n",
|
|
" print(\"rapidfuzz is not installed. Please install it with 'pip install rapidfuzz' to use fuzzy matching.\")\n",
|
|
" process = None\n",
|
|
" fuzz = None\n",
|
|
"\n",
|
|
"# Get unique countries\n",
|
|
"unique_countries = dirty_data['country'].unique()\n",
|
|
"\n",
|
|
"# For each country, find similar matches\n",
|
|
"if process is not None and fuzz is not None:\n",
|
|
" print(\"Finding similar country names (similarity > 70%):\")\n",
|
|
" for country in unique_countries:\n",
|
|
" matches = process.extract(country, unique_countries, scorer=fuzz.ratio, limit=3)\n",
|
|
" # Filter matches with similarity > 70 and not identical\n",
|
|
" similar = [m for m in matches if m[1] > 70 and m[0] != country]\n",
|
|
" if similar:\n",
|
|
" print(f\"\\n'{country}' is similar to:\")\n",
|
|
" for match, score, _ in similar:\n",
|
|
" print(f\" - '{match}' (similarity: {score}%)\")\n",
|
|
"else:\n",
|
|
" print(\"Skipping fuzzy matching because rapidfuzz is not available.\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 2. అసాధారణ సంఖ్యా విలువలను గుర్తించడం (అవుట్లయర్స్)\n",
|
|
"\n",
|
|
"`age` కాలమ్ను చూస్తే, 199 మరియు -5 వంటి కొన్ని అనుమానాస్పద విలువలు ఉన్నాయి. ఈ అవుట్లయర్స్ను గుర్తించడానికి గణాంక పద్ధతులను ఉపయోగిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Display basic statistics\n",
|
|
"print(\"Age column statistics:\")\n",
|
|
"print(dirty_data['age'].describe())\n",
|
|
"\n",
|
|
"# Identify impossible values using domain knowledge\n",
|
|
"print(\"\\nRows with impossible age values (< 0 or > 120):\")\n",
|
|
"impossible_ages = dirty_data[(dirty_data['age'] < 0) | (dirty_data['age'] > 120)]\n",
|
|
"print(impossible_ages[['customer_id', 'name', 'age']])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### IQR (ఇంటర్కార్టైల్ రేంజ్) పద్ధతి ఉపయోగించడం\n",
|
|
"\n",
|
|
"IQR పద్ధతి అత్యంత విలువలకు తక్కువ సున్నితమైన, అవుట్లయర్ గుర్తింపు కోసం బలమైన గణాంక పద్ధతి.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Calculate IQR for age (excluding impossible values)\n",
|
|
"valid_ages = dirty_data[(dirty_data['age'] >= 0) & (dirty_data['age'] <= 120)]['age']\n",
|
|
"\n",
|
|
"Q1 = valid_ages.quantile(0.25)\n",
|
|
"Q3 = valid_ages.quantile(0.75)\n",
|
|
"IQR = Q3 - Q1\n",
|
|
"\n",
|
|
"# Define outlier bounds\n",
|
|
"lower_bound = Q1 - 1.5 * IQR\n",
|
|
"upper_bound = Q3 + 1.5 * IQR\n",
|
|
"\n",
|
|
"print(f\"IQR-based outlier bounds for age: [{lower_bound:.2f}, {upper_bound:.2f}]\")\n",
|
|
"\n",
|
|
"# Identify outliers\n",
|
|
"age_outliers = dirty_data[(dirty_data['age'] < lower_bound) | (dirty_data['age'] > upper_bound)]\n",
|
|
"print(f\"\\nRows with age outliers:\")\n",
|
|
"print(age_outliers[['customer_id', 'name', 'age']])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Z-స్కోర్ పద్ధతి ఉపయోగించడం\n",
|
|
"\n",
|
|
"Z-స్కోర్ పద్ధతి సగటు నుండి స్టాండర్డ్ డివియేషన్ల ఆధారంగా అవుట్లయర్లను గుర్తిస్తుంది:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"try:\n",
|
|
" from scipy import stats\n",
|
|
"except ImportError:\n",
|
|
" print(\"scipy is required for Z-score calculation. Please install it with 'pip install scipy' and rerun this cell.\")\n",
|
|
"else:\n",
|
|
" # Calculate Z-scores for age, handling NaN values\n",
|
|
" age_nonan = dirty_data['age'].dropna()\n",
|
|
" zscores = np.abs(stats.zscore(age_nonan))\n",
|
|
" dirty_data['age_zscore'] = np.nan\n",
|
|
" dirty_data.loc[age_nonan.index, 'age_zscore'] = zscores\n",
|
|
"\n",
|
|
" # Typically, Z-score > 3 indicates an outlier\n",
|
|
" print(\"Rows with age Z-score > 3:\")\n",
|
|
" zscore_outliers = dirty_data[dirty_data['age_zscore'] > 3]\n",
|
|
" print(zscore_outliers[['customer_id', 'name', 'age', 'age_zscore']])\n",
|
|
"\n",
|
|
" # Clean up the temporary column\n",
|
|
" dirty_data = dirty_data.drop('age_zscore', axis=1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### అవుట్లయర్స్ను నిర్వహించడం\n",
|
|
"\n",
|
|
"ఒకసారి గుర్తించిన తర్వాత, అవుట్లయర్స్ను అనేక విధాలుగా నిర్వహించవచ్చు:\n",
|
|
"1. **తొలగించు**: అవుట్లయర్స్ ఉన్న వరుసలను తొలగించండి (వీటిని తప్పులుగా ఉంటే)\n",
|
|
"2. **క్యాప్ చేయండి**: సరిహద్దు విలువలతో మార్చండి\n",
|
|
"3. **NaN తో మార్చండి**: లేని డేటాగా పరిగణించి ఇంప్యూటేషన్ సాంకేతికతలను ఉపయోగించండి\n",
|
|
"4. **ఉంచండి**: అవి చట్టబద్ధమైన అత్యంత విలువలు అయితే\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create a cleaned version by replacing impossible ages with NaN\n",
|
|
"dirty_data['age_clean'] = dirty_data['age'].apply(\n",
|
|
" lambda x: np.nan if (x < 0 or x > 120) else x\n",
|
|
")\n",
|
|
"\n",
|
|
"print(\"Age column before and after cleaning:\")\n",
|
|
"print(dirty_data[['customer_id', 'name', 'age', 'age_clean']])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 3. సమీప-నకిలీ వరుసలను గుర్తించడం\n",
|
|
"\n",
|
|
"మన డేటాసెట్లో \"జాన్ స్మిత్\" కోసం కొంత భిన్నమైన విలువలతో అనేక ఎంట్రీలు ఉన్నాయని గమనించండి. పేరుల సమానత ఆధారంగా సంభావ్య నకిలీలను గుర్తిద్దాం.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# First, let's look at exact name matches (ignoring extra whitespace)\n",
|
|
"dirty_data['name_normalized'] = dirty_data['name'].str.strip().str.lower()\n",
|
|
"\n",
|
|
"print(\"Checking for duplicate names:\")\n",
|
|
"duplicate_names = dirty_data[dirty_data.duplicated(['name_normalized'], keep=False)]\n",
|
|
"print(duplicate_names.sort_values('name_normalized')[['customer_id', 'name', 'age', 'country']])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### ఫజీ మ్యాచ్ చేయడం ద్వారా సమీప-నకిలీలను కనుగొనడం\n",
|
|
"\n",
|
|
"మరింత సున్నితమైన నకిలీ గుర్తింపు కోసం, సమానమైన పేర్లను కనుగొనడానికి ఫజీ మ్యాచ్ చేయడాన్ని ఉపయోగించవచ్చు:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"try:\n",
|
|
" from rapidfuzz import process, fuzz\n",
|
|
"\n",
|
|
" # Function to find potential duplicates\n",
|
|
" def find_near_duplicates(df, column, threshold=90):\n",
|
|
" \"\"\"\n",
|
|
" Find near-duplicate entries in a column using fuzzy matching.\n",
|
|
" \n",
|
|
" Parameters:\n",
|
|
" - df: DataFrame\n",
|
|
" - column: Column name to check for duplicates\n",
|
|
" - threshold: Similarity threshold (0-100)\n",
|
|
" \n",
|
|
" Returns: List of potential duplicate groups\n",
|
|
" \"\"\"\n",
|
|
" values = df[column].unique()\n",
|
|
" duplicate_groups = []\n",
|
|
" checked = set()\n",
|
|
" \n",
|
|
" for value in values:\n",
|
|
" if value in checked:\n",
|
|
" continue\n",
|
|
" \n",
|
|
" # Find similar values\n",
|
|
" matches = process.extract(value, values, scorer=fuzz.ratio, limit=len(values))\n",
|
|
" similar = [m[0] for m in matches if m[1] >= threshold]\n",
|
|
" \n",
|
|
" if len(similar) > 1:\n",
|
|
" duplicate_groups.append(similar)\n",
|
|
" checked.update(similar)\n",
|
|
" \n",
|
|
" return duplicate_groups\n",
|
|
"\n",
|
|
" # Find near-duplicate names\n",
|
|
" duplicate_groups = find_near_duplicates(dirty_data, 'name', threshold=90)\n",
|
|
"\n",
|
|
" print(\"Potential duplicate groups:\")\n",
|
|
" for i, group in enumerate(duplicate_groups, 1):\n",
|
|
" print(f\"\\nGroup {i}:\")\n",
|
|
" for name in group:\n",
|
|
" matching_rows = dirty_data[dirty_data['name'] == name]\n",
|
|
" print(f\" '{name}': {len(matching_rows)} occurrence(s)\")\n",
|
|
" for _, row in matching_rows.iterrows():\n",
|
|
" print(f\" - Customer {row['customer_id']}: age={row['age']}, country={row['country']}\")\n",
|
|
"except ImportError:\n",
|
|
" print(\"rapidfuzz is not installed. Skipping fuzzy matching for near-duplicates.\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### డూప్లికేట్లను నిర్వహించడం\n",
|
|
"\n",
|
|
"ఒకసారి గుర్తించిన తర్వాత, మీరు డూప్లికేట్లను ఎలా నిర్వహించాలో నిర్ణయించుకోవాలి:\n",
|
|
"1. **మొదటి సంభవాన్ని ఉంచండి**: `drop_duplicates(keep='first')` ఉపయోగించండి\n",
|
|
"2. **చివరి సంభవాన్ని ఉంచండి**: `drop_duplicates(keep='last')` ఉపయోగించండి\n",
|
|
"3. **సమాచారాన్ని సమ్మేళనం చేయండి**: డూప్లికేట్ వరుసల నుండి సమాచారాన్ని కలపండి\n",
|
|
"4. **మాన్యువల్ సమీక్ష**: మానవ సమీక్ష కోసం ఫ్లాగ్ చేయండి\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Example: Remove duplicates based on normalized name, keeping first occurrence\n",
|
|
"cleaned_data = dirty_data.drop_duplicates(subset=['name_normalized'], keep='first')\n",
|
|
"\n",
|
|
"print(f\"Original dataset: {len(dirty_data)} rows\")\n",
|
|
"print(f\"After removing name duplicates: {len(cleaned_data)} rows\")\n",
|
|
"print(f\"Removed: {len(dirty_data) - len(cleaned_data)} duplicate rows\")\n",
|
|
"\n",
|
|
"print(\"\\nCleaned dataset:\")\n",
|
|
"print(cleaned_data[['customer_id', 'name', 'age', 'country_clean']])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### సారాంశం: పూర్తి డేటా శుభ్రపరిచే పైప్లైన్\n",
|
|
"\n",
|
|
"మనం అన్నింటినీ ఒక సమగ్ర శుభ్రపరిచే పైప్లైన్లో కలపుదాం:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def clean_dataset(df):\n",
|
|
" \"\"\"\n",
|
|
" Comprehensive data cleaning function.\n",
|
|
" \"\"\"\n",
|
|
" # Create a copy to avoid modifying the original\n",
|
|
" cleaned = df.copy()\n",
|
|
" \n",
|
|
" # 1. Standardize categorical values (country)\n",
|
|
" country_mapping = {\n",
|
|
" 'usa': 'USA', 'u.s.a': 'USA', 'united states': 'USA',\n",
|
|
" 'uk': 'UK', 'united kingdom': 'UK',\n",
|
|
" 'canada': 'Canada', 'mexico': 'Mexico'\n",
|
|
" }\n",
|
|
" cleaned['country'] = cleaned['country'].str.lower().map(country_mapping)\n",
|
|
" \n",
|
|
" # 2. Clean abnormal age values\n",
|
|
" cleaned['age'] = cleaned['age'].apply(\n",
|
|
" lambda x: np.nan if (x < 0 or x > 120) else x\n",
|
|
" )\n",
|
|
" \n",
|
|
" # 3. Remove near-duplicate names (normalize whitespace)\n",
|
|
" cleaned['name'] = cleaned['name'].str.strip()\n",
|
|
" cleaned = cleaned.drop_duplicates(subset=['name'], keep='first')\n",
|
|
" \n",
|
|
" return cleaned\n",
|
|
"\n",
|
|
"# Apply the cleaning pipeline\n",
|
|
"final_cleaned_data = clean_dataset(dirty_data)\n",
|
|
"\n",
|
|
"print(\"Before cleaning:\")\n",
|
|
"print(f\" Rows: {len(dirty_data)}\")\n",
|
|
"print(f\" Unique countries: {dirty_data['country'].nunique()}\")\n",
|
|
"print(f\" Invalid ages: {((dirty_data['age'] < 0) | (dirty_data['age'] > 120)).sum()}\")\n",
|
|
"\n",
|
|
"print(\"\\nAfter cleaning:\")\n",
|
|
"print(f\" Rows: {len(final_cleaned_data)}\")\n",
|
|
"print(f\" Unique countries: {final_cleaned_data['country'].nunique()}\")\n",
|
|
"print(f\" Invalid ages: {((final_cleaned_data['age'] < 0) | (final_cleaned_data['age'] > 120)).sum()}\")\n",
|
|
"\n",
|
|
"print(\"\\nCleaned dataset:\")\n",
|
|
"print(final_cleaned_data[['customer_id', 'name', 'age', 'country', 'purchase_amount']])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 🎯 సవాలు వ్యాయామం\n",
|
|
"\n",
|
|
"ఇప్పుడు మీ వారం! క్రింద ఒక కొత్త డేటా వరుస ఉంది, ఇందులో అనేక నాణ్యత సమస్యలు ఉన్నాయి. మీరు చేయగలరా:\n",
|
|
"\n",
|
|
"1. ఈ వరుసలోని అన్ని సమస్యలను గుర్తించండి\n",
|
|
"2. ప్రతి సమస్యను శుభ్రం చేయడానికి కోడ్ రాయండి\n",
|
|
"3. శుభ్రం చేసిన వరుసను డేటాసెట్లో జోడించండి\n",
|
|
"\n",
|
|
"ఇది సమస్యాత్మక డేటా:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# New problematic row\n",
|
|
"new_row = pd.DataFrame({\n",
|
|
" 'customer_id': [13],\n",
|
|
" 'name': [' Diana Prince '], # Extra whitespace\n",
|
|
" 'age': [250], # Impossible age\n",
|
|
" 'country': ['U.S.A.'], # Inconsistent format\n",
|
|
" 'purchase_amount': [150.00]\n",
|
|
"})\n",
|
|
"\n",
|
|
"print(\"New row to clean:\")\n",
|
|
"print(new_row)\n",
|
|
"\n",
|
|
"# TODO: Your code here to clean this row\n",
|
|
"# Hints:\n",
|
|
"# 1. Strip whitespace from the name\n",
|
|
"# 2. Check if the name is a duplicate (Diana Prince already exists)\n",
|
|
"# 3. Handle the impossible age value\n",
|
|
"# 4. Standardize the country name\n",
|
|
"\n",
|
|
"# Example solution (uncomment and modify as needed):\n",
|
|
"# new_row_cleaned = new_row.copy()\n",
|
|
"# new_row_cleaned['name'] = new_row_cleaned['name'].str.strip()\n",
|
|
"# new_row_cleaned['age'] = np.nan # Invalid age\n",
|
|
"# new_row_cleaned['country'] = 'USA' # Standardized\n",
|
|
"# print(\"\\nCleaned row:\")\n",
|
|
"# print(new_row_cleaned)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### ముఖ్యమైన విషయాలు\n",
|
|
"\n",
|
|
"1. **అసమానమైన వర్గాలు** వాస్తవ ప్రపంచ డేటాలో సాధారణం. ఎప్పుడూ ప్రత్యేక విలువలను తనిఖీ చేసి, మ్యాపింగ్లు లేదా ఫజీ మ్యాచ్ చేయడం ద్వారా వాటిని ప్రమాణీకరించండి.\n",
|
|
"\n",
|
|
"2. **అత్యంత విలక్షణాలు** మీ విశ్లేషణపై గణనీయంగా ప్రభావం చూపవచ్చు. వాటిని గుర్తించడానికి డొమైన్ జ్ఞానం మరియు గణాంక పద్ధతులు (IQR, Z-స్కోర్) ఉపయోగించండి.\n",
|
|
"\n",
|
|
"3. **సమీప-నకిలీలు** ఖచ్చితమైన నకిలీల కంటే గుర్తించడం కష్టం. వాటిని గుర్తించడానికి ఫజీ మ్యాచ్ చేయడం మరియు డేటాను సాధారణీకరించడం (తక్కువ అక్షరాలు, ఖాళీలను తొలగించడం) పరిగణించండి.\n",
|
|
"\n",
|
|
"4. **డేటా శుభ్రపరిచే ప్రక్రియ పునరావృతం**. మీరు అనేక సాంకేతికతలను వరుసగా ఉపయోగించి, ఫలితాలను సమీక్షించి, చివరికి శుభ్రపరిచిన డేటాసెట్ను నిర్ధారించాలి.\n",
|
|
"\n",
|
|
"5. **మీ నిర్ణయాలను డాక్యుమెంట్ చేయండి**. మీరు ఏ శుభ్రపరిచే దశలను ఎందుకు ఉపయోగించారో ట్రాక్ చేయండి, ఇది పునరుత్పత్తి మరియు పారదర్శకతకు ముఖ్యమైనది.\n",
|
|
"\n",
|
|
"> **ఉత్తమ ఆచారం:** ఎప్పుడూ మీ అసలు \"కురుపు\" డేటా కాపీని ఉంచండి. మీ మూల డేటా ఫైళ్లను ఎప్పుడూ మళ్లీ రాయవద్దు - స్పష్టమైన పేర్లతో శుభ్రపరిచిన వెర్షన్లను సృష్టించండి, ఉదాహరణకు `data_cleaned.csv`.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n\n<!-- CO-OP TRANSLATOR DISCLAIMER START -->\n**అస్పష్టత**: \nఈ పత్రాన్ని AI అనువాద సేవ [Co-op Translator](https://github.com/Azure/co-op-translator) ఉపయోగించి అనువదించబడింది. మేము ఖచ్చితత్వానికి ప్రయత్నించినప్పటికీ, ఆటోమేటెడ్ అనువాదాల్లో పొరపాట్లు లేదా తప్పిదాలు ఉండవచ్చు. మూల పత్రం దాని స్వదేశీ భాషలో అధికారిక మూలంగా పరిగణించాలి. ముఖ్యమైన సమాచారానికి, ప్రొఫెషనల్ మానవ అనువాదం సిఫార్సు చేయబడుతుంది. ఈ అనువాదం వాడకంలో ఏర్పడిన ఏవైనా అపార్థాలు లేదా తప్పుదారితీసే అర్థాలు కోసం మేము బాధ్యత వహించము.\n<!-- CO-OP TRANSLATOR DISCLAIMER END -->\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"anaconda-cloud": {},
|
|
"colab": {
|
|
"name": "notebook.ipynb",
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.5.4"
|
|
},
|
|
"coopTranslator": {
|
|
"original_hash": "6301339d1c9a301b00639c635dc9b731",
|
|
"translation_date": "2025-12-19T17:24:11+00:00",
|
|
"source_file": "2-Working-With-Data/08-data-preparation/notebook.ipynb",
|
|
"language_code": "te"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
} |