【问题标题】:Pandas: FutureWarning: Passing list-likes to .loc or [] [duplicate]熊猫:FutureWarning:将列表喜欢传递给 .loc 或 [] [重复]
【发布时间】:2018-10-18 04:31:52
【问题描述】:

当我运行我的脚本时,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”,也会出现同样的警告。我不知道如何重新索引?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    似乎需要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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 2020-06-13
      • 2021-07-21
      • 2021-03-05
      • 2021-07-18
      • 2020-12-24
      • 2020-07-06
      相关资源
      最近更新 更多