Pandas:FutureWarning:将列表喜欢传递给 .loc 或 [] [重复]

2024-02-29

当我运行脚本时,Pandas 给出以下内容:“未来警告”

FutureWarning:
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative. 

我的脚本:

import io

data = io.StringIO('''A,B,M
AM,1,
AMC,2,
''')
df = pd.read_csv(data)

r=['CAR']
s=['CAR_M']

for i,j in zip(r,s):
    df=df.append([{'A':i,'M':j}], ignore_index=True)

如果“ignore_index=False”,则会出现相同的警告。我不知道如何重新索引?


看来需要Series正确追加新行的构造函数:

for i,j in zip(r,s):
    df=df.append(pd.Series({'A':i,'M':j}), ignore_index=True)

print (df)
     A    B      M
0   AM  1.0    NaN
1  AMC  2.0    NaN
2  CAR  NaN  CAR_M
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pandas:FutureWarning:将列表喜欢传递给 .loc 或 [] [重复] 的相关文章

随机推荐