python pandas data frame error while trying to print it within single df[ _ , _ ] form
Question:
I see dataframe error while trying to print it within single df[ _ , _ ] form. Below are the code lines
#Data Frames code
import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
df =pd.DataFrame(randArr,np.arange(101,106,1),['PDS','Algo','SE','INS'])
print(df['PDS','SE'])
errors:
Traceback (most recent call last): File "C:UserssubroAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespandascoreindexesbase.py", line 3621, in get_loc return self._engine.get_loc(casted_key) File "pandas_libsindex.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc File "pandas_libsindex.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc File "pandas_libshashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libshashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: ('PDS', 'SE')
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "D:Education4th year1st semMachine Learning Lab1st LabpythonpandaspdDataFrame.py", line 11, in <module> print(df['PDS','SE']) File "C:UserssubroAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespandascoreframe.py", line 3505, in __getitem__ indexer = self.columns.get_loc(key) File "C:UserssubroAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespandascoreindexesbase.py", line 3623, in get_loc raise KeyError(key) from err KeyError: ('PDS', 'SE')
Answers:
Do you mean to do this? Need to indicate the column names when creating the dataframe, and also need double square brackets df[[ ]]
when extracting a slice of the dataframe
import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
df = pd.DataFrame(randArr, columns=['PDS', 'SE', 'ABC', 'CDE'])
print(df)
print(df[['PDS','SE']])
Output:
PDS SE ABC CDE
0 56 77 82 42
1 17 12 84 46
2 34 9 19 12
3 19 88 34 19
4 51 54 9 94
PDS SE
0 56 77
1 17 12
2 34 9
3 19 88
4 51 54
use print(df[['PDS','SE']])
format instead of print(df['PDS','SE'])
I see dataframe error while trying to print it within single df[ _ , _ ] form. Below are the code lines
#Data Frames code
import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
df =pd.DataFrame(randArr,np.arange(101,106,1),['PDS','Algo','SE','INS'])
print(df['PDS','SE'])
errors:
Traceback (most recent call last): File "C:UserssubroAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespandascoreindexesbase.py", line 3621, in get_loc return self._engine.get_loc(casted_key) File "pandas_libsindex.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc File "pandas_libsindex.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc File "pandas_libshashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libshashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: ('PDS', 'SE')
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "D:Education4th year1st semMachine Learning Lab1st LabpythonpandaspdDataFrame.py", line 11, in <module> print(df['PDS','SE']) File "C:UserssubroAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespandascoreframe.py", line 3505, in __getitem__ indexer = self.columns.get_loc(key) File "C:UserssubroAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespandascoreindexesbase.py", line 3623, in get_loc raise KeyError(key) from err KeyError: ('PDS', 'SE')
Do you mean to do this? Need to indicate the column names when creating the dataframe, and also need double square brackets df[[ ]]
when extracting a slice of the dataframe
import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
df = pd.DataFrame(randArr, columns=['PDS', 'SE', 'ABC', 'CDE'])
print(df)
print(df[['PDS','SE']])
Output:
PDS SE ABC CDE
0 56 77 82 42
1 17 12 84 46
2 34 9 19 12
3 19 88 34 19
4 51 54 9 94
PDS SE
0 56 77
1 17 12
2 34 9
3 19 88
4 51 54
use print(df[['PDS','SE']])
format instead of print(df['PDS','SE'])