1

I have a dataframe like this:

No     Data    Sentence
32      xxx      yyyy
45      hhh      uuuu
 .       .        . 
 .       .        . 
8726    aaa      bbbb  

Where the No column is unordered, now I have x which is list of sentences and I want to add that list to the Sentence column after my last index. So my new dataframe will look like:

No     Data    Sentence
32      xxx      yyyy
45      hhh      uuuu
    .       .         . 
    .       .         . 
8726    aaa      bbbb
NaN     NaN      x[0]
NaN     NaN      x[1]
 .       .         .
 .       .         .
NaN     NaN      x[n]

I know we can directly assign list to column by assign function but it'll assign list value from beginning and I don't even know size of my list. Can someone help me with this?

2 Answers 2

1

Convert the list of sentences to a dataframe and then use pd.concat:

x = pd.DataFrame({'Sentence': x})
pd.concat([df,x], axis=0, ignore_index=True)
Sign up to request clarification or add additional context in comments.

2 Comments

exactly what I was looking for. Thank you very muchh for help
@VIBHUBAROT: Happy to help :)
1

First turn your list into a dataframe:

listDF = pd.DataFrame(list, columns='Sentence')

Then use pandas.DataFrame.append

extended_df = df.appned(listDF)

To test:

create df1:

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB')) 

print it:

   A  B
0  1  2
1  3  4

create df2:

df2 = pd.DataFrame([5, 6], columns=list('B'))

print it:

   B
0  5
1  6

finally:

df.append(df2)

results:

     A  B
0  1.0  2
1  3.0  4
0  NaN  5
1  NaN  6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.