'pandas' code example

Example 1: pandas dataframe

>>> df2 = pd.DataFrame(np.array([[23, 99, 78], [65, 95, 90], [90, 98, 96]]),
...                    columns=['krish', 'ishwar', 'raj'])
>>> df2
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

Example 2: pandas in python

If you use Anaconda then it is preinstalled
otherwise: pip install pandas

import pandas as pd
pd.__version__

Example 3: panda python

# install pandas (basic, if path is not set yet)
py -m pip install pandas
# or set PATH to use pip:
setx PATH "%PATH%;C:\<path\to\python\directory\>\Scripts"
pip install pandas
# if "connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" [!]:
py -m pip install --trusted-host pypi.python.org pip pandas
# if PermissionError: [WinError 5] Access is denied
py -m pip install --user pandas
# or via creating a virtual environment venv:
py -m venv c:\path\to\new\environment
# then execute:
c:\path\to\new\environment\Scripts\activate.bat

Example 4: pandas

#for dropping a column in a dataframe
df = df.drop(['PassengerId'], axis = 1)

#for selecting all columns except one
df.iloc[:, df.columns != "Survived"]

#for checking nan values is a column
df['your column name'].isnull()

Example 5: pandas

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
   .....:                   columns=['A', 'B', 'C', 'D'])
   .....: 

In [137]: df = df.cumsum()

In [138]: plt.figure()
Out[138]: <Figure size 640x480 with 0 Axes>

In [139]: df.plot()
Out[139]: <matplotlib.axes._subplots.AxesSubplot at 0x7f53723daa10>

In [140]: plt.legend(loc='best')
Out[140]: <matplotlib.legend.Legend at 0x7f5369d93dd0>

Tags: