Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (18.4k points)

I need to fill the data frame gradually. in each step, I have the data like this:

pubid = 1

keywords = [2, 2,3]

knowing that length of values for each column are not equal how can I form the data frame like this:

pubid   keyword

1         2

1         2

1         3

So next time when my new data is coming and is like this:

pubid = 6

keywords = [10, 11]

my data frame has become as shown:

pubid   keyword

1         2

1         2

1         3

6         10

6         11

I tried to create the temp dataframe at every beginning and add some values like this:

data = {'pubid': 1, 'keywords':[1]}

df = pd.DataFrame(data)

pubid = 3

keyword = [2, 3]

df['pubid'] = 3

df["keywords"] = df["pubid"].apply(lambda x: i for i in keyword)

It does not work in that way, but don't know how to solve it.

1 Answer

0 votes
by (36.8k points)

Use the below code: 

pubid = 1

keywords = [2, 2,3]

df = pd.DataFrame({'pubid': pubid, 'keywords': keywords})

print(df)

Prints:

   pubid  keywords

0      1         2

1      1         2

2      1         3

Then you can the use pd.concat to add data to existing DataFrame:

pubid = 6

keywords = [10, 11]

df = pd.concat([df, pd.DataFrame({'pubid': pubid, 'keywords': keywords})]).reset_index(drop=True)

print(df)

Prints:

   pubid  keywords

0      1         2

1      1         2

2      1         3

3      6        10

4      6        11

 Want to be a master in Data Science? Enroll in this Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...