Python 在中添加列
我很难将一个特定的列“幸福平均值”形式的变量“熊猫”类型的pd\u dictionary添加到另一个变量“熊猫”类型的pd\u dictionary\u treat 我试着用磁带记录下我的代码,在下面的代码中加上(幸福指数):Python 在中添加列,python,pandas,Python,Pandas,我很难将一个特定的列“幸福平均值”形式的变量“熊猫”类型的pd\u dictionary添加到另一个变量“熊猫”类型的pd\u dictionary\u treat 我试着用磁带记录下我的代码,在下面的代码中加上(幸福指数): import pandas as pd pd_dictionnary=pd.read_csv("/Users/stefanhanssen/Dropbox/hse/word_list/131278/Data_Set_S1.txt",sep="\t") pd_dictio
import pandas as pd
pd_dictionnary=pd.read_csv("/Users/stefanhanssen/Dropbox/hse/word_list/131278/Data_Set_S1.txt",sep="\t")
pd_dictionnary_treat=pd_dictionnary["word"]
pd_dictionnary_treat.append(pd_dictionnary["happiness_average"])
print(pd_dictionnary_treat)
我的预期结果是在变量pd\u dictionnary\u treat
中有两列:
第一个:pd\u dictionnary[“word”]
第二个:pd\u dictionnary[“幸福平均值”]
实际结果是变量pd\u dictionnary\u treat
中的一列,即pd\u dictionnary[“word”]
这是我的数据集1.txt文件
为什么要使用
append
功能?
您可以在将word
列指定给pd\u dictionary\u treat
时直接添加。
我认为这将有助于你:
import pandas as pd
pd_dictionnary=pd.read_csv("/Users/stefanhanssen/Dropbox/hse/word_list/131278/Data_Set_S1.txt",sep="\t")
pd_dictionnary_treat= pd_dictionnary[["word","happiness_average"]]
print(pd_dictionnary_treat)
pd\u dictionnary\u treat=pd\u dictionnary[[“单词”,“幸福”平均值]]
?除此之外,我不明白这个问题。似乎df中已经有了“happiness\u average”
,否则我不知道它来自哪里。请提供您的输入csv文件和预期的输出示例@Stefan HanssenDo是否要在数据框底部添加新行?确实如此。我认为@roganjosh建议的内容更符合您的要求。@roganjosh我尝试了您的代码,但它不起作用,我尝试将“幸福平均值”列从变量pd_dictionnary添加到变量pd_dictionnary_treat@Nisheet帕特尔,我只是在描述中加上它