简体   繁体   English

如何为数据框的新列添加字符串作为值

[英]How to add string as value for new column for dataframe

I am trying to add string value and assign to a new column for dataframe , but could not figure it out the best way to do this. 我正在尝试添加字符串值并将其分配给dataframe的新列,但无法找出执行此操作的最佳方法。

Df['new'] = "my value"

But this is not working and I couldn't understand why, can someone please explain to me ? 但这是行不通的,我不明白为什么,有人可以向我解释吗? Or how exactly can I do this in Python using pandas dataframe ? 或者我该如何在Python中使用pandas dataframe做到这一点?

Do you mean this? 你是这个意思吗

import pandas as pd

d = {'a':[1,2,3], 'b':[4,5,6]}
j = {'c':['a','hello','t']}
x = pd.DataFrame(d)
y = pd.DataFrame(j)
x.insert(2, 'c', y)

暂无
暂无

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

相关问题 如何在数据框的新列中添加值? - How add a value in a new column in a dataframe? 如果在列文本字符串中找到值,如何使用字典键添加新的 Dataframe 列 - How to add new Dataframe Column with Dictionary Key, if the Value is found in a column text string 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 使用空字符串或A列中的值(取决于B列中的值)在pandas数据框中添加新列 - Add new column in pandas dataframe using empty string or the value from column A depending on the value on column B 如何从 json 类型系列(字符串)添加新的 dataframe 列 - How to add new dataframe column from json type series(string) 如何根据外部列表的值在 dataframe 中添加新列? - How to add a new column in a dataframe based on the value of external list? 如何使用日志值向 Pandas 数据框添加新列 - How to add new column to Pandas dataframe with log value Python:在DataFrame中,在新列中为另一列中具有最高值的行添加值,在第三列中添加相同的字符串 - Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one 如何在 pandas dataframe 中为具有字符串条件的新列分配值 - How to assign a value to a new column with a string condition in pandas dataframe 如何比较数据框列中的字符串值和单元格中的值以基于多值字典创建新的数据框? - How to compare string value in dataframe column and value in cell to create new dataframe based on multi value dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM