简体   繁体   English

如何将新列添加到现有 pandas dataframe

[英]How to add a new column to an existing pandas dataframe

I got a pandas data frame with spam messages.我收到了一个带有垃圾邮件的 pandas 数据帧。

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.但是,如果我想将它作为列添加到我的数据框中,它只会显示 NaN 值。 Why?为什么? And how can I solve this issue?我该如何解决这个问题?

You can add a new row using append function您可以使用 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) 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.问题是:数字仍然显示为 NaN 值。

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我希望,我现在可以说得更清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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