<jupyter_start><jupyter_text>Pandas DataFrame<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Q1. Write a Pandas program to convert the given array's into dataframe    Sample data: {'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}    Expected Output:      X  Y  Z    0 78 84 86    1 85 94 97    2 96 89 96    3 80 83 72    4 86 86 83<jupyter_code>d={'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}
pd.DataFrame(d)<jupyter_output><empty_output><jupyter_text>Q2. Write a Pandas program to create and display a DataFrame from a specified dictionary data which has the index labels.<jupyter_code># Sample DataFrame:
exam_data = {
    'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
    'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
    'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
    'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df=pd.DataFrame(exam_data,index=labels)
df<jupyter_output><empty_output><jupyter_text>Q3. Write a Pandas program to display a summary of the basic "information" about the above DataFrame and its data.<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Index: 10 entries, a to j
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   name      10 non-null     object 
 1   score     8 non-null      float64
 2   attempts  10 non-null     int64  
 3   qualify   10 non-null     object 
dtypes: float64(1), int64(1), object(2)
memory usage: 400.0+ bytes<jupyter_text>Q4. Write a Pandas program to get the first 3 rows of above DataFrame.<jupyter_code># Sol-1
df.head(3)
# Sol-2
df.iloc[:3]<jupyter_output><empty_output><jupyter_text>Q5. Write a Pandas program to select the 'name' and 'score' columns from the above DataFrame.<jupyter_code># Sol - 1
df.loc[:,['name','score']]
# Sol - 2: using the list notation to read more than one column
df[['name','score']]<jupyter_output><empty_output><jupyter_text>Q6. Write a Pandas program to select the specified columns and rows from the above DataFrame.Select 'name' and 'score' columns in rows 1, 3, 5, 6 from the following data frame.<jupyter_code># Sol - 1
df.loc[["b","d","f","g"],['name','score']]
# Sol - 2 
df.iloc[[1, 3, 5, 6], [1, 3]]<jupyter_output><empty_output><jupyter_text>Q7. Write a Pandas program to select the rows where the number of attempts in the examination is greater than 2.<jupyter_code># Sol-1
df.loc[df['attempts']>2]
# Sol-2
df.query("attempts>2")
# Sol-3
df.where(df.attempts>2).dropna(axis = 0,how = 'all')<jupyter_output><empty_output><jupyter_text>Q8. Write a Pandas program to count the number of rows and columns of a DataFrame.<jupyter_code># No of rows - Count doesn't include NaN values
# Sol-1
df.name.count()
# Sol-2
len(df.axes[0])
## len(df) --> this will count all the values including NaN.
# No of columns
# Sol-1
len(df.columns)
# Sol-2
len(df.axes[1])<jupyter_output><empty_output><jupyter_text>Q9. Write a Pandas program to select the rows where the score is missing, i.e. is NaN.<jupyter_code>#Sol-1
df[df.score.isna()]
# Sol-2
df[df['score'].isnull()]<jupyter_output><empty_output><jupyter_text>Q10. Write a Pandas program to select the rows the score is between 15 and 20 (inclusive).<jupyter_code># Sol-1
df[(df['score']>15) & (df['score']<=20)]
# Sol-2
df[df['score'].between(15, 20)]
# Sol-3 using query
df.query('score >= 15 and score <= 20')
df<jupyter_output><empty_output><jupyter_text>Q11. Write a Pandas program to select the rows where number of attempts in the examination is less than 2 and score greater than 15.<jupyter_code>df[(df['attempts']<2) & (df['score']>15)]
df.query('attempts < 2 and score > 15')<jupyter_output><empty_output><jupyter_text>Q12. Write a Pandas program to change the score in row 'd' to 11.5.<jupyter_code># Sol - 1
df.loc['d',['score']]=11.5
df.loc[['c','d'],:]
df<jupyter_output><empty_output><jupyter_text>Q13. Write a Pandas program to calculate the sum of the examination attempts by all the students.<jupyter_code>df.attempts.sum()<jupyter_output><empty_output><jupyter_text>Q14. Write a Pandas program to calculate the mean score for each different student in data frame.<jupyter_code>df.score.mean()
df.describe()<jupyter_output><empty_output><jupyter_text>Q15. Write a Pandas program to append a new row 'k' to DataFrame with given values for each column. Now delete the new row and return the original data frame.<jupyter_code># Sol-1 
# ** DF.append will append the data frame, but it will not keep it in place, so we have to reassign the appended data frame to the same DF. 
# inplace = True option is not there in DF.append. In lists we can append without reassign. Same the case with DF.merge.

df2=pd.DataFrame([['Tom',10.0,1,'Yes']], index=['k'],columns=['name', 'score', 'attempts', 'qualify'])
df.append(df2)
df.loc['k']=['Tom',10,1,'Yes']
df
df.drop('k', inplace=True)
df<jupyter_output><empty_output><jupyter_text>Q16. Write a Pandas program to sort the data frame first by 'name' in descending order, then by 'score' in ascending order.<jupyter_code># ** 5.	Shorting with multi columns is possible. 
df.sort_values(by=['name', 'score'], ascending=[False, True])<jupyter_output><empty_output><jupyter_text>Q17. Write a Pandas program to replace the ‘qualify' column contains the values 'yes' and 'no' with True and False.<jupyter_code># Sol-1
df.loc[df['qualify']=='yes',['qualify']]=True
df.loc[df['qualify']=='no',['qualify']]=False
df
exam_data  = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)

# Sol-2 ** Use df.column.map(dict()) to replace all value in that column with another or same value. 
# ** For replacing multiple values to one unique value use df column.replace('yes', 'True')
df['qualify'] = df['qualify'].map({'yes': True, 'no': False})
df
# Sol -3 Using replace
#df['qualify'].replace({'yes':True, 'no':False}, inplace=True) --> This will give a type error. 

df['qualify'].replace(to_replace='yes', value=None, method='pad')
df.head()<jupyter_output><empty_output><jupyter_text>Q18. Write a Pandas program to change the name 'James' to 'Suresh' in name column of the data frame.<jupyter_code>df.loc[df['name']=='James',['name']]='Suresh'
df
df['name'].replace('James', 'Suresh')
df<jupyter_output><empty_output><jupyter_text>Q19. Write a Pandas program to delete the 'attempts' column from the DataFrame.<jupyter_code># Sol-1
df.drop(columns=['attempts'])
# Sol-2
df.pop('attempts')
df<jupyter_output><empty_output><jupyter_text>Q20. Write a Pandas program to insert a new column color in existing DataFrame.<jupyter_code>color = ['Red','Blue','Orange','Red','White','White','Blue','Green','Green','Red']
df['color']=color
df<jupyter_output><empty_output><jupyter_text>Q21. Write a Pandas program to iterate over rows in a DataFrame and print row name and row score    sample output:    Anastasia 12.5                                                             Dima 9.0                                                                   Katherine 16.5<jupyter_code># Sample Python dictionary data and list labels:
exam_data = [{'name':'Anastasia', 'score':12.5}, {'name':'Dima','score':9}, {'name':'Katherine','score':16.5}] 
df1=pd.DataFrame(exam_data)

#Sol-1
for index, i in df1.iterrows():  # ** DF can be itterable using df.iterrows()
  print(i['name'], i['score'])
# Sol-2
t=zip(df1.name,df1.score)

for i,j in t:
  print(i,j)
t1=t
for i,j in t:
  print(i,j)   # ** Zip function is not displacing output

print(t1==t)<jupyter_output>True<jupyter_text>Q22. Write a Pandas program to get list from DataFrame column headers.<jupyter_code>list(df.columns.values)<jupyter_output><empty_output><jupyter_text>Q23. Write a Pandas program to rename columns of a given DataFrame.<jupyter_code>d = {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
new_names=['Column1', 'Column2', 'Column3']
df5=pd.DataFrame(d)
df5
# Sol-1
df5=df5.rename(columns={'col1':'Column1','col2':'Column2','col3':'Column3'}) 
# ** inplace option not avilable. so self assign the data afterrename  
df5
d = {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
df5=pd.DataFrame(d)

# Sol-2
col=dict(zip(d.keys(),new_names))
df5.rename(columns = col)<jupyter_output><empty_output><jupyter_text>Q24. Write a Pandas program to select rows from a given DataFrame based on values in some columns.filter the rows of colum1 value == 4<jupyter_code>d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
df6 = pd.DataFrame(data=d)
df6
df6[df6['col1']==4]<jupyter_output><empty_output><jupyter_text>Q25. Write a Pandas program to change the order of a above DataFrame columns to 'col3', 'col2', 'col1'<jupyter_code># Sol-1
df6.reindex(columns=['col3', 'col2', 'col1'])
# Sol-2
df6[['col3', 'col2', 'col1']]<jupyter_output><empty_output><jupyter_text>Q26. Write a Pandas program to add one row with values 10, 11, 12 in an above DataFrame.<jupyter_code>#df6.iloc
df6.loc[5]=[10,11,12]
df6<jupyter_output><empty_output><jupyter_text>Q27. Write a Pandas program to write above DataFrame to CSV file using tab separator.<jupyter_code>df6.to_csv('op.csv',sep='\t', index=False) 
#** keep index = False as best practice, we will get index as a new column else while reading it back 
pd.read_csv('/content/op.csv',sep='\t')<jupyter_output><empty_output><jupyter_text>Q28. Write a Pandas program to count city wise number of people from a given of data set (city, name of the person).              city  Number of people    0   California                 4    1      Georgia                 2    2  Los Angeles                 4<jupyter_code>df7 = pd.DataFrame({'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'city': ['California', 'Los Angeles', 'California', 'California', 'California', 'Los Angeles', 'Los Angeles', 'Georgia', 'Georgia', 'Los Angeles']})
df7
df7.groupby('city').size().reset_index(name='Number of people')
# ** count will give only if values are present in rows.
# ** size will give even if some rows are nan<jupyter_output><empty_output><jupyter_text>Q29. Write a Pandas program to delete DataFrame row(s) where value of co12 is not equal to 5<jupyter_code>d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
df6 = pd.DataFrame(data=d)
df6
# Sol 1
df6[~df6.col2.isin([5])]
# Sol 2
df6[df6.col2 != 5]<jupyter_output><empty_output><jupyter_text>Q30. Write a Pandas program to widen output display to see more columns.<jupyter_code>d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
df6 = pd.DataFrame(data=d)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)  # ** This option is used if we have more columns
pd.set_option('display.width', 1000)
print("Original DataFrame")
df6<jupyter_output>Original DataFrame<jupyter_text>Q31. Write a Pandas program to select a row of dataframe by given integer index.select and display the row with Index=2 of the above DF<jupyter_code>df6.iloc[2,:]<jupyter_output><empty_output><jupyter_text>Q32. Write a Pandas program to replace all the NaN values with Zero's in a column of a dataframe.<jupyter_code>exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
df = pd.DataFrame(exam_data)
df
df.fillna(0)<jupyter_output><empty_output><jupyter_text>Q33. Write a Pandas program to convert index in to a column of the above dataframe.<jupyter_code>df.reset_index()<jupyter_output><empty_output><jupyter_text>Q34. Write a Pandas program to set a value 10.2 for a value 8 in score column using index value.<jupyter_code># Sol-1
df.loc[df['score']==8,['score']]=10.2
df
# Sol-2
df.score.replace(8.0,10.2, inplace=True)
df
# we cant apply using map function, as map function will apply function to all rows and replace with Nan<jupyter_output><empty_output><jupyter_text>Q35. Write a Pandas program to count the NaN values in one or more columns in DataFrame.<jupyter_code>df.isna().values.sum()<jupyter_output><empty_output><jupyter_text>Q36. Write a Pandas program to drop a list of rows from a specified DataFrame.<jupyter_code>d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
df6 = pd.DataFrame(d)
drop_rows=[2,4]
df6
df6.drop(index=drop_rows)<jupyter_output><empty_output><jupyter_text>Q37. Write a Pandas program to reset index in a given DataFrame after removing the first two rows<jupyter_code>df6.drop(index=[0,1]).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>Q38. Write a Pandas program to devide a DataFrame in a ratio 70 : 30<jupyter_code>df8 = pd.DataFrame(np.random.randn(10, 2)) # Return a sample from std normal distribution of given size
df8
part_70=df8.sample(frac=0.7,random_state=10)
part_70
df8.drop(part_70.index)  # ** .index will give all the index of perticular DF<jupyter_output><empty_output><jupyter_text>Q39. Write a Pandas program to combining two series into a DataFrame.<jupyter_code>s1 = pd.Series(['100', '200', 'python', '300.12', '400'])
s2 = pd.Series(['10', '20', 'php', '30.12', '40'])
# Sol-1
pd.DataFrame([s1,s2]).T
# Sol-2
pd.concat([s1,s2],axis=1)<jupyter_output><empty_output><jupyter_text>Q40. Write a Pandas program to shuffle a given DataFrame rows.<jupyter_code>exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
df = pd.DataFrame(exam_data)
df
df.sample(frac=1)<jupyter_output><empty_output><jupyter_text>Q41. Write a Pandas program to convert DataFrame column type from string to datetime.<jupyter_code>s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'])
s
pd.to_datetime(s)<jupyter_output><empty_output><jupyter_text>Q42. Write a Pandas program to rename a col2 to Column2 in a given DataFrame.<jupyter_code>d = {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
df5 = pd.DataFrame(data=d)
df5
df5.rename(columns={'col2':'Column2'})<jupyter_output><empty_output><jupyter_text>Q43. Write a Pandas program to get a list of values in col2 in above DataFrame.<jupyter_code># SOl-1
list(df5.col2.values)
# Sol-2
df5.col2.to_list()<jupyter_output><empty_output><jupyter_text>Q44. Write a Pandas program to create a DataFrame from a Numpy array and specify the index column and column headers.    Sample Output              Column1  Column2  Column3    Index1         0      0.0      0.0    Index2         0      0.0      0.0    Index3         0      0.0      0.0    Index4         0      0.0      0.0    Index5         0      0.0      0.0    Index6         0      0.0      0.0    Index7         0      0.0      0.0    Index8         0      0.0      0.0    Index9         0      0.0      0.0    Index10        0      0.0      0.0    Index11        0      0.0      0.0    Index12        0      0.0      0.0    Index13        0      0.0      0.0    Index14        0      0.0      0.0    Index15        0      0.0      0.0<jupyter_code>dtype = [('Column1','int32'), ('Column2','float32'), ('Column3','float32')] # Datatype of each column
values=np.zeros(15,dtype=dtype) # ** zeros(shape, dtype=float) # ** shape can be achieved even by dtype
values
index = ['Index'+str(i) for i in range(1, len(values)+1)]
index
pd.DataFrame(values, index=index)<jupyter_output><empty_output><jupyter_text>Q45. Write a Pandas program to find the row for where the value of a each col2 is maximum.<jupyter_code>d = {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
df5 = pd.DataFrame(data=d)
df5
# Sol1-1
print('max values:', df5.col2.max())
print('ID of max values:', df5.col2.idxmax())
# Sol-2
df5['col2'].argmax()<jupyter_output><empty_output><jupyter_text>Q46. Write a Pandas program to check whether a col4, col1 are present in above DataFrame or not.<jupyter_code>if 'col4' in df.columns:
  print("Col4 is present in DataFrame.")
else:
  print("Col4 is not present in DataFrame.")
if 'col1' in df.columns:
  print("Col1 is present in DataFrame.")
else:
  print("Col1 is not present in DataFrame.")<jupyter_output>Col1 is not present in DataFrame.<jupyter_text>Q47. Write a Pandas program to get the row1 value of above DataFrame.<jupyter_code>df5.iloc[[1],:]<jupyter_output><empty_output><jupyter_text>Q48. Write a Pandas program to get the datatypes of columns of a DataFrame.<jupyter_code>exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
df = pd.DataFrame(exam_data)
df
# Sol-1
df.info()
# Sol-2
df.dtypes<jupyter_output><empty_output><jupyter_text>Q49. Write a Pandas program to append data to an empty DataFrame.<jupyter_code>df0 = pd.DataFrame()
df0.append(df)<jupyter_output><empty_output><jupyter_text>Q50. Write a Pandas program to sort above DataFrame by two columns: attempts, name.<jupyter_code>df.sort_values(['attempts', 'name'], ascending=[True, True])<jupyter_output><empty_output><jupyter_text>Q51. Write a Pandas program to convert the datatype of a score column from floats to int.<jupyter_code>df.fillna(0,inplace=True)
df.score = df.score.astype(int)
df<jupyter_output><empty_output><jupyter_text>Q52. Write a Pandas program to replace infinite values with Nan values.<jupyter_code>df4 = pd.DataFrame([1000, 2000, 3000, -4000, np.inf, -np.inf])
df4
df4.replace([np.inf, -np.inf], np.nan) 
# ** for inf always use np.inf
# ** For replacing multiple values to one unique value use replace<jupyter_output><empty_output><jupyter_text>Q53. Write a Pandas program to insert a col1 at a column index 0 in a DataFrame.<jupyter_code>d = {'col2': [4, 5, 6, 9, 5], 'col3': [7, 8, 12, 1, 11]}
col1 = [1, 2, 3, 4, 7] 
df5 = pd.DataFrame(data=d)
df5
# Sol-1
df5['col1']=col1
df5=df5[['col1','col2','col3']]
df5
d = {'col2': [4, 5, 6, 9, 5], 'col3': [7, 8, 12, 1, 11]}
col1 = [1, 2, 3, 4, 7] 
df5 = pd.DataFrame(data=d)

# SOl-2
df5.insert(loc=0, column='col1', value=col1)
df5<jupyter_output><empty_output><jupyter_text>Q54. Write a Pandas program to convert a given list of lists into a Dataframe. use 0th list as df column names<jupyter_code>my_lists = [['col1', 'col2'], [2, 4], [1, 3]]
pd.DataFrame(my_lists[1:3],columns=my_lists[0]) 
# ** Slicing of list will not include end value eg[1:3], where as slicing of pandas dataframes include end value eg df.loc[[1:3],:]<jupyter_output><empty_output><jupyter_text>Q55. Write a Pandas program to group by the first column and get second column as lists in rows.    output:    Group on the col1:    col1    C1          [1, 2]    C2    [3, 3, 4, 5]    C3             [6]<jupyter_code>df10 = pd.DataFrame( {'col1':['C1','C1','C2','C2','C2','C3','C2'], 'col2':[1,2,3,3,4,6,5]})
df10
df10.groupby('col1')['col2'].apply(list)  # ** after groupby we call other columns instead of applying agg functions<jupyter_output><empty_output><jupyter_text>Q56. Write a Pandas program to get column index for col2.<jupyter_code>d = {'col1': [1, 2, 3, 4, 7], 'col2': [4, 5, 6, 9, 5], 'col3': [7, 8, 12, 1, 11]}
df5 = pd.DataFrame(data=d)
df5
df5.columns.get_loc("col2")<jupyter_output><empty_output><jupyter_text>Q57. Write a Pandas program to count number of columns of above DataFrame.<jupyter_code>len(df5.columns)<jupyter_output><empty_output><jupyter_text>Q58. Write a Pandas program to select all columns, except col3 in above DataFrame.<jupyter_code># Sol-1
df5.drop(columns=['col3'])
# Sol-2
df5.loc[:, df5.columns != 'col3'] # ** we can apply conditions to filter columns instead of passing columns list<jupyter_output><empty_output><jupyter_text>Q59. Write a Pandas program to get first 3 records of above DataFrame<jupyter_code># Sol-1
df5.iloc[0:3,:]
# Sol-2
df5.head(3)<jupyter_output><empty_output><jupyter_text>Q60. Write a Pandas program to get last 3 records of a DataFrame.<jupyter_code>df5.tail(3)<jupyter_output><empty_output><jupyter_text>Q61. Write a Pandas program to get topmost 3 records within each column (col1,col2,col3) of above DataFrame.<jupyter_code>df5.nlargest(3, 'col1') # Col1
df5.sort_values('col2',ascending=False).head(3) # Col2
df5.nlargest(3, 'col3') # Col3<jupyter_output><empty_output><jupyter_text>Q62. Write a Pandas program to remove first 3 rows of above given DataFrame.<jupyter_code># Sol-1
df5.drop(index=[0,1,2])
# Sol-2
df5.iloc[3:]<jupyter_output><empty_output><jupyter_text>Q63. Write a Pandas program to remove last 3 rows of a given DataFrame.<jupyter_code># Sol-1
df5.iloc[:2]
#Sol-2  ** If index unkown or to avoid manual listing of index
l=df5.tail(3).index.tolist()  # gives [2, 3, 4]
df5.drop(index=l)<jupyter_output><empty_output><jupyter_text>Q64. Write a Pandas program to add a prefix or suffix to all columns of a given DataFrame.    output:    Add prefix:       A_W  A_X  A_Y  A_Z    0   68   78   84   86    ....    Add suffix:       W_1  X_1  Y_1  Z_1    0   68   78   84   86    ....<jupyter_code>df3 = pd.DataFrame({'W':[68,75,86,80,66],'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});
df3
#Sol-1
l=df3.columns.values.tolist()
p=['A_'+i for i in l]
p
df3.columns=['A_W', 'A_X', 'A_Y', 'A_Z']
df3
s=[i+'_1' for i in l]
s
df3.columns=['A_W', 'A_X', 'A_Y', 'A_Z']
df3
df3 = pd.DataFrame({'W':[68,75,86,80,66],'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});
df3

# Sol-2
df3.add_prefix("A_")
df3.add_suffix("_1")<jupyter_output><empty_output><jupyter_text>Q65. Write a Pandas program to reverse order (rows, columns) of above DataFrame.    Reverse column order:        Z   Y   X   W    0  86  84  78  68    1  97  94  85  75    2  96  89  96  86    3  72  83  80  80    4  83  86  86  66    Reverse row order:        W   X   Y   Z    4  66  86  86  83    3  80  80  83  72    2  86  96  89  96    1  75  85  94  97    0  68  78  84  86<jupyter_code>df3.loc[:, ::-1]
df3.loc[::-1]<jupyter_output><empty_output><jupyter_text>Q66. Write a Pandas program to select columns by data type (number, object) of a given DataFrame.<jupyter_code>exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
df = pd.DataFrame(exam_data)
df
df.select_dtypes(include = "number") # **
df.select_dtypes(include = "object")<jupyter_output><empty_output><jupyter_text>Q67. Write a Pandas program to split above DataFrame into two random subsets.<jupyter_code>df_1 = df.sample(frac = 0.6)
df_1
df_2 = df.drop(df_1.index)
df_2<jupyter_output><empty_output><jupyter_text>Q68. Write a Pandas program to rename all column names by Remove trailing (at the end) whitesapce and convert to lowercase.<jupyter_code>df = pd.DataFrame({
    'Name': ['<NAME>','<NAME>','<NAME>', '<NAME>', '<NAME>'],
    'Date_Of_Birth ': ['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
    'Age': [18.5, 21.2, 22.5, 22, 23]
})
df
df.columns = df.columns.str.lower()
df<jupyter_output><empty_output><jupyter_text>Q69. Write a Pandas program to merge datasets df_3, df_4 and check uniqueness.<jupyter_code>df11 = pd.DataFrame({
    'Name': ['<NAME>','<NAME>','<NAME>', '<NAME>', '<NAME>'],
    'Date_Of_Birth ': ['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
    'Age': [18.5, 21.2, 22.5, 22, 23]
})

#"one_to_one”: check if merge keys are unique in both left and right datasets:"'
#"one_to_many” or “1:m”: check if merge keys are unique in left dataset:
#'“many_to_one” or “m:1”: check if merge keys are unique in right dataset:''
df11_1 = df11.copy(deep=True)
df11_1
df11_2 = df11_1.drop([0, 1])
df11_2
df11_3 = df11_1.drop([2])
df11_3
#"one_to_one”: check if merge keys are unique in both left and right datasets:"'
df_one_to_one = pd.merge(df11_2, df11_3, validate = "one_to_one")
df_one_to_one
# "one_to_many” or “1:m”: check if merge keys are unique in left dataset:
df_one_to_many = pd.merge(df11_2, df11_3, validate = "one_to_many")
df_one_to_many
# “many_to_one” or “m:1”: check if merge keys are unique in right dataset:
df_many_to_one = pd.merge(df11_2, df11_3, validate = "many_to_one")
df_many_to_one<jupyter_output><empty_output><jupyter_text>Q70. Write a Pandas program to convert continuous values of a column in above DataFrame to categorical.<jupyter_code>df12 = pd.DataFrame({
    'name': ['<NAME>','<NAME>','<NAME>', '<NAME>', '<NAME>', '<NAME>'],
      'age': [18, 22, 85, 50, 80, 5]
})
df12
df12["age_groups"] = pd.cut(df12["age"], bins = [0, 18, 65, 99], labels = ["kids", "adult", "elderly"])
df12["age_groups"] # cut assign the lables wwhere value matches the proper bin<jupyter_output><empty_output><jupyter_text>Q71. Write a Pandas program to display memory usage of a given DataFrame and every column of the DataFrame.<jupyter_code># Global usage of memory of the DataFrame
df.info(memory_usage = "deep") # Last line memory usage
# The usage of memory of every column of the said DataFrame:
df.memory_usage(deep = True)<jupyter_output><empty_output><jupyter_text>Q72. Write a Pandas program to combine many given series to create a DataFrame.<jupyter_code>sr1 = pd.Series(['php', 'python', 'java', 'c#', 'c++'])
sr2 = pd.Series([1, 2, 3, 4, 5])
# Sol-1
pd.DataFrame([sr1, sr2]).T
# Sol-2
pd.concat([sr1, sr2], axis = 1)<jupyter_output><empty_output><jupyter_text>Q73. Write a Pandas program to create DataFrames that contains random values, contains missing values, contains datetime values and contains mixed values.<jupyter_code>df13 = pd.util.testing.makeDataFrame() # contains random values
df13
df14 = pd.util.testing.makeMissingDataframe() # contains missing values
df14
df15 = pd.util.testing.makeTimeDataFrame() # contains datetime values
df15
df16 = pd.util.testing.makeMixedDataFrame() # contains mixed values
df16<jupyter_output><empty_output><jupyter_text>Q74. Write a Pandas program to fill missing values in time series data.From Wikipedia , in the mathematical field of numerical analysis, interpolation is a type of estimation, a method of constructing new data points within the range of a discrete set of known data points.<jupyter_code>sdata = {"c1":[120, 130 ,140, 150, np.nan, 170], "c2":[7, np.nan, 10, np.nan, 5.5, 16.5]}
df17 = pd.DataFrame(sdata)
df17
df17.index = pd.util.testing.makeDateIndex()[0:6]
df17
df17.interpolate()<jupyter_output><empty_output><jupyter_text>Q75. Write a Pandas program to use a local variable within a query.<jupyter_code>df18 = pd.DataFrame({'W':[68,75,86,80,66],'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});
df18
maxx = df18["W"].max()
df18.query("W < @maxx") # Values which are less than maximum value of 'W' column"
df18<jupyter_output><empty_output><jupyter_text>Q76. Write a Pandas program to clean object column with mixed data of a given DataFrame using regular expression.<jupyter_code>d = {"agent": ["a001", "a002", "a003", "a003", "a004"], "purchase":[4500.00, 7500.00, "$3000.25", "$1250.35", "9000.00"]}
df19 = pd.DataFrame(d)
df19
df19["purchase"].apply(type)
df19["purchase"] = df19["purchase"].replace("[$]", "", regex = True).astype("float")
df19<jupyter_output><empty_output><jupyter_text>Q77. Write a Pandas program to get the numeric representation of an array by identifying distinct values of a given column of a DataFrame.<jupyter_code>df20 = pd.DataFrame({
    'Name': ['<NAME>','<NAME>','<NAME>', '<NAME>', '<NAME>'],
    'Date_Of_Birth ': ['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
    'Age': [18.5, 21.2, 22.5, 22, 23]
})
df20
label1, unique1 = pd.factorize(df20['Name']) # **
label1
unique1<jupyter_output><empty_output><jupyter_text>Q78. Write a Pandas program to replace the current value in a dataframe column based on last largest value. If the current value is less than last largest value replaces the value with 0.<jupyter_code>df21=pd.DataFrame({'rnum':[23, 21, 27, 22, 34, 33, 34, 31, 25, 22, 34, 19, 31, 32, 19]})
df21
df21['rnum']=df21.rnum.where(df21.rnum.eq(df21.rnum.cummax()),0)
df21<jupyter_output><empty_output><jupyter_text>Q79. Write a Pandas program to create a DataFrame from the clipboard (data from an Excel spreadsheet or a Google Sheet).<jupyter_code># ** read_clipboard will not work in cloud notebooks, as cant fetch from ur local system. works in local jupyter notebook
# df22 = pd.read_clipboard() 
# df22<jupyter_output><empty_output><jupyter_text>Q80.Write a Pandas program to check for inequality of two given DataFrames.<jupyter_code>df23 = pd.DataFrame({'W':[68,75,86,80,None],'X':[78,85,None,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});
df24 = pd.DataFrame({'W':[78,75,86,80,None],'X':[78,85,96,80,76], 'Y':[84,84,89,83,86],'Z':[86,97,96,72,83]});
df23
df24
df23.ne(df24) # **<jupyter_output><empty_output><jupyter_text>Q81. Write a Pandas program to get lowest n records within each column of a given DataFrame.<jupyter_code>d = {'col1': [1, 2, 3, 4, 7, 11], 'col2': [4, 5, 6, 9, 5, 0], 'col3': [7, 5, 8, 12, 1,11]}
df25 = pd.DataFrame(data=d)
df25
df26 = df25.nsmallest(3, 'col1')
print(df26)
df27 = df25.nsmallest(3, 'col2')
print(df27)
df28 = df25.nsmallest(3, 'col3')
print(df28)<jupyter_output>col1  col2  col3
0     1     4     7
1     2     5     5
2     3     6     8
   col1  col2  col3
5    11     0    11
0     1     4     7
1     2     5     5
   col1  col2  col3
4     7     5     1
1     2     5     5
0     1     4     7
{'fraction_non_alphanumeric': 0.16205131570313014, 'fraction_numerical': 0.06896778003806024, 'mean_word_length': 4.62654605870408, 'pattern_counts': {'":': 4, '<': 351, '<?xml version=': 0, '>': 355, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '82', 'dir': 'jupyter-structured-clean-dedup', 'id': '109668', 'license': 'MIT', 'n_tokens_mistral': 12777, 'n_tokens_neox': 11638, 'n_words': 3313, 'path': 'Copy_of_Pandas_Dataframe_Answers.ipynb', 'repo_name': 'reachanu21/AdvancedTimeseries', 'size': '30609'}