Informatics Practices
From the DataFrames created in previous question, write code to add an empty column 'x' to all DataFrames.
Python Pandas
2 Likes
Answer
import pandas as pd
d = {'one' : pd.Series([1., 2., 3.], index = ['a', 'b', 'c']), 'two' : pd.Series([1., 2., 3., 4.], index = ['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
df1 = pd.DataFrame(d, index = ['d', 'b', 'a'])
df2 = pd.DataFrame(d, index = ['d', 'a'], columns = ['two', 'three'])
df['x'] = None
df1['x'] = None
df2['x'] = None
print(df)
print(df1)
print(df2)Output
one two x
a 1.0 1.0 None
b 2.0 2.0 None
c 3.0 3.0 None
d NaN 4.0 None
one two x
d NaN 4.0 None
b 2.0 2.0 None
a 1.0 1.0 None
two three x
d 4.0 NaN None
a 1.0 NaN None
Answered By
1 Like
Related Questions
From the DataFrames created in previous question, write code to display only rows 0 and 1 from DataFrames df, df1, and df2.
From the DataFrames created in previous question, write code to display only rows 'a' and 'b' for columns 1 and 2 from DataFrames df, df1 and df2.
What will be the output of the following program ?
import pandas as pd dic = {'Name' : ['Sapna', 'Anmol', 'Rishul', 'Sameep'], 'Agg' : [56, 67, 75, 76], 'Age' : [16, 18, 16, 19]} df = pd.DataFrame(dic, columns = ['Name', 'Age']) print(df)(a)
Name Agg Age 101 Sapna 56 16 102 Anmol 67 18 103 Rishul 75 16 104 Sameep 76 19(b)
Name Agg Age 0 Sapna 56 16 1 Anmol 67 18 2 Rishul 75 16 3 Sameep 76 19(c)
Name 0 Sapna 1 Anmol 2 Rishul 3 Sameep(d)
Name Age 0 Sapna 16 1 Anmol 18 2 Rishul 16 3 Sameep 19Predict the output of following code (it uses below given dictionary my_di).
my_di = {"name" : ["Jiya", "Tim", "Rohan"], "age" : np.array([10, 15, 20]), "weight" : (75, 123, 239), "height" : [4.5, 5, 6.1], "siblings" : 1, "gender" : "M"}df = pd.DataFrame(my_di) print(df)