简体   繁体   中英

How to add a new column to an existing pandas dataframe

I got a pandas data frame with spam messages.

I want to create an extra column next to the column of the messages and show the amount of words each message has.

For Example:

Index      Content           Amount of words
0          Hi I am cool      4
1          What up?          2
2          Are you happy?    3

I can count the amount of words per each message:

count = data['INHALT'].str.split().str.len()
count.index = count.index.astype(str) + ' words:'

But if I want to add it as a column to my data frame, it only shows me NaN-values. Why? And how can I solve this issue?

You can add a new row using append function

df = df.append(new_row, ignore_index=True)

Ah I think I've made a mistake.

because by using the code

my_dataframe.append(row_to_append, ignore_index = True)

I add every number to a row. But actually I want ONE row with the name "numer of words" and then every number should be added for every message in each column.

The problem is: the numbers are still shown as NaN values.

This is my column I want to add:

DOC_ID
0 words:     1125
1 words:      745
2 words:     1874
3 words:     1129
4 words:     1614
             ... 
78 words:    1649
79 words:     872
80 words:    1624
81 words:     866
82 words:    1327
Name: INHALT, Length: 83, dtype: int64

I just want the numbers as a row called "number of words" to be add next to the row "messages"

Again the example:

Index      Content           Amount of words
0          Hi I am cool       4
1          What up?           2
2          Are you happy?     3

I hope, that I could make it more clear right now

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM