in Education by
I am trying to combine 2 columns into my dataframe using a new column which is of type JSON. To do so i have used itertuples() but it is taking a lot of time can anyone suggest me a better approch, here is the data mentioned below: fn = ('Mike', 'Dorothee') dob = ('1985-08-07', '1987-01-33') data = ({'rank':'5', 'subject':'science'}, {'rank':'1', 'subject':'arts'}) df = pd.DataFrame(list(zip(fn, dob, data)), columns =['fn', 'dob', 'data']) for row in df.itertuples(): df.loc[row.Index, 'result'] = str(dict([(row.dob, row.data)])) +----------+------------+-------------------------------------+-----------------------------------------------------+ | fn | dob | data | result | |----------+------------+-------------------------------------+-----------------------------------------------------| | Mike | 1985-08-07 | {'rank': '5', 'subject': 'science'} | {'1985-08-07': {'rank': '5', 'subject': 'science'}} | | Dorothee | 1987-01-33 | {'rank': '1', 'subject': 'arts'} | {'1987-01-33': {'rank': '1', 'subject': 'arts'}} | +----------+------------+-------------------------------------+-----------------------------------------------------+ Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can use df.apply() df['result'] = df.apply(lambda x: {x.dob: x.data}, axis=1) Improve your knowledge in data science from scratch using Data science online courses

Related questions

0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a list of files in a folder in my system file_list= ["A", "B", "C"] I Have read the files using ... C How do I accomplish this? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have 2 data frames df1 Name 2010 2011 0 Jack 25 35 1 Jill 15 20 df2 Name 2010 2011 0 Berry 45 25 1 ... used the code df1.add(df2) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I have the dataframe as below: df ID val 1 0.0 2 yes 3 1.0 4 0.0 5 yes How do I match my ... use np.where along with the bfill? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have a dataframe that looks like this: from to datetime other ---------------------------------- ... !! Thank you so much in advance! Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am trying to alter the data in the panda's df. Using below, where X >=5, I want to change the corresponding Y row to 1. Where X...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the below data frame lst = [['A',1],['B',0],['C',1],['D',0],['E',1],['F',1],['G',1]] ... give all val with 1. df[df.val == 1] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    To the data frame df: Player Team Points Mean Price Value Gameweek 1 Jim Leeds 4.4 4.40 10.44 0.44 2 Jim ... scalar What am I missing? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I have the list like this where items are separated by ":". x=['john:42:engineer', 'michael:29:doctor' ... engineer 1 michael 29 doctor Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have the df df = pd.DataFrame({'col1': [1,2,3,4,5,6]}) and I would like to use df.isin() and ... help me find the solution to it? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have 2 columns in the python dataframe. I want to check each row in my Column A for any value that ... for this particular purpose. Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
...