在dataframe中循环的空列中添加值的正确方法,python

时间:2019-05-17 11:56:37

标签: python pandas dataframe

df=pd.DataFrame[columns='one','two','three']

for home in city:
    adres= home
    for a in abc: #abc is pd.Series od names
        #here i want to add values - adress and a , but my dataframe have 3 columns, i will use only 2 here
         df.loc[len(df)]= [adres, a, np.nan]
          print(df)
one, two, three
alabama, flat, NaN

我应该如何为adres的{​​{1}}和a列中添加值onetwo,并使列df不可触摸? / p>

谢谢

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找类似的东西:

for i in range(1):
    df.loc[i,['one','two']]=['adress','a']
print(df)

      one two three
0  adress   a   NaN

答案 1 :(得分:1)

我首先创建一个熊猫系列,其中包含我想要的值和列。例如:

new_values= pd.Series(["a", "b"], index=["one", "two"])

然后我将该系列附加到原始数据框:

df= df.append(new_values, ignore_index=True)