I have a dataframe in the form of
column 1 column 2 column 3
And I would like to add values to it. I have a list which I would like to add which is in the form of:
a= [['Master Vithal', ' Vithal Zubeida'], ['Firozshah Mistry', ' B Irani'], ['Grigor']]
How do I add elements in this list so that a[0] will go to column 1 and so on. Thanks in advance!
You can use .loc
to add elements of list a
at the end of the dataframe, as follows:
import numpy as np
cols = ['column 1', 'column 2', 'column 3'] # enter your selected columns here
df.loc[len(df), cols] = np.array(a, dtype='object')[0:len(cols)]
Demo
import numpy as np
df = pd.DataFrame(columns=['column 1', 'column 2', 'column 3'])
a= [['Master Vithal', ' Vithal Zubeida'], ['Firozshah Mistry', ' B Irani'], ['Grigor']]
cols = ['column 1', 'column 2', 'column 3']
df.loc[len(df), cols] = np.array(a, dtype='object')[0:len(cols)]
print(df)
column 1 column 2 column 3
0 [Master Vithal, Vithal Zubeida] [Firozshah Mistry, B Irani] [Grigor]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.