Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试将序列附加到我的数据帧时语法无效_Python_Pandas - Fatal编程技术网

Python 尝试将序列附加到我的数据帧时语法无效

Python 尝试将序列附加到我的数据帧时语法无效,python,pandas,Python,Pandas,我用一些我认识的人的名字、他们和我的关系以及他们的生日月份制作了一个数据框。我想和我妈妈一起在这个数据框中添加一个新的系列 import pandas as pd entry_one = pd.Series({'Name':'Ryan', 'Relationship':'Self', 'Birthday_Month' : 'April'}) entry_two = pd.Seri

我用一些我认识的人的名字、他们和我的关系以及他们的生日月份制作了一个数据框。我想和我妈妈一起在这个数据框中添加一个新的系列

import pandas as pd
entry_one = pd.Series({'Name':'Ryan',
                      'Relationship':'Self',               
                      'Birthday_Month' : 'April'})
entry_two = pd.Series({'Name':'Nickie',
                      'Relationship': 'Girlfriend',
                      'Birthday_Month':'February'})
entry_three = pd.Series({'Name':'Jeff',
                        'Relationship':'Friend',
                        'Birthday_Month':'August'})
entry_four = pd.Series ({'Name':'Mica',
                        'Relationship':'Friend',
                        'Birthday_Month':'November'})
entry_five = pd.Series ({'Name':'Andrew',
                        'Relationship':'Friend',
                        'Birthday_Month':'November'})

df = pd.DataFrame([entry_one, entry_two, entry_three, entry_four, entry_five])
df = df.set_index('Name')
df
df.append(pd.Series(data = ('Name':'Mom','Relationship':'Mother','Birthday_Month':'May')))
但是我得到了错误

错误

文件“”,第1行 append(pd.Series(数据=('Name':'Mom','Relationship':'Mom','birth\u Month':'May')) ^ SyntaxError:无效语法


我在这里做错了什么?

您需要
字典
而不需要
名称
,它被用作
系列
的参数
名称

df = pd.DataFrame([entry_one, entry_two, entry_three, entry_four, entry_five])
df = df.set_index('Name')
df = df.append(pd.Series(data = {'Relationship':'Mother','Birthday_Month':'May'},name='Mom'))
动态解决方案无需更改
字典
,只需过滤出
名称
,并通过dict的
键选择系列名称

d = {'Name':'Mom','Relationship':'Mother','Birthday_Month':'May'}
df = df.append(pd.Series(data = {k:v for k,v in d.items() if k!= 'Name'},name=d['Name']))

或者在没有设置索引的情况下使用
忽略索引=True

df = pd.DataFrame([entry_one, entry_two, entry_three, entry_four, entry_five])
d = {'Name':'Mom','Relationship':'Mother','Birthday_Month':'May'}
df = df.append(pd.Series(data=d), ignore_index=True)
print (df)
  Birthday_Month    Name Relationship
0          April    Ryan         Self
1       February  Nickie   Girlfriend
2         August    Jeff       Friend
3       November    Mica       Friend
4       November  Andrew       Friend
5            May     Mom       Mother

您在最后一行中使用了错误类型的括号,它需要是一个字典,并且您需要启用“忽略”(ignore)索引才能像这样追加:

df.append(pd.Series(data = {'Name':'Mom','Relationship':'Mother','Birthday_Month':'May'}), ignore_index=True)

如果我的答案有用,别忘了-点击复选标记(
),将其从灰显切换为填充。谢谢。请看另一个答案,它的质量要好得多,你的答案没有添加任何重要信息。
df.append(pd.Series(data = {'Name':'Mom','Relationship':'Mother','Birthday_Month':'May'}), ignore_index=True)