1# append row to dataframe without index
2
3a_row = pd.Series([1, 2])
4df = pd.DataFrame([[3, 4], [5, 6]])
5
6row_df = pd.DataFrame([a_row])
7df = pd.concat([row_df, df], ignore_index=True)
8
9print(df)
10# OUTPUT
11# 0 1
12# 0 1 2
13# 1 3 4
14# 2 5 6
15
16# append row to dataframe with index
17
18a_row = pd.Series([1, 2])
19df = pd.DataFrame([[3, 4], [5, 6]], index = ["row1", "row2"])
20
21row_df = pd.DataFrame([a_row], index = ["row3"])
22df = pd.concat([row_df, df])
23
24print(df)
25# OUTPUT
26# 0 1
27# row3 1 2
28# row1 3 4
29# row2 5 6
1import pandas as pd
2import numpy as np
3x=pd.DataFrame([{'BOY':1,'GIRL':44},{'BOY':22,'GIRL':100}])
4print(x)
5x=x.T #TRANSPOSE IT AND MAKE IT AS COLUMN
6x.insert(1,2,[44,56]) #INSERT A NEW COLUMN AT ANY POSITION
7x=x.T # NOW TRANSPOSE IT AGAIN TO MAKE IT ROW AGAIN
8x=x.reset_index(drop=True) # RESET INDEX
9print(x)
1line = DataFrame({"onset": 30.0, "length": 1.3}, index=[3])
2df2 = concat([df.iloc[:2], line, df.iloc[2:]]).reset_index(drop=True)
1# Add a new row at index k with values provided in list
2dfObj.loc['k'] = ['Smriti', 26, 'Bangalore', 'India']
3
1In [99]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])
2
3In [100]: s = df.xs(3)
4
5In [101]: s.name = 10
6
7In [102]: df.append(s)
8Out[102]:
9 A B C D
100 -2.083321 -0.153749 0.174436 1.081056
111 -1.026692 1.495850 -0.025245 -0.171046
122 0.072272 1.218376 1.433281 0.747815
133 -0.940552 0.853073 -0.134842 -0.277135
144 0.478302 -0.599752 -0.080577 0.468618
155 2.609004 -1.679299 -1.593016 1.172298
166 -0.201605 0.406925 1.983177 0.012030
177 1.158530 -2.240124 0.851323 -0.240378
1810 -0.940552 0.853073 -0.134842 -0.277135
19