在dataframe中循环的空列中添加值的正确方法,python [英] right way to add values in empty column in loop in dataframe , python
本文介绍了在dataframe中循环的空列中添加值的正确方法,python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
和 a
到 df中的
并保留列一个
和两个
列三个
不可触摸?
How propery should i add values adres
and a
to one
and two
column in df
and leave column three
untouchable?
谢谢
推荐答案
我首先创建一个熊猫系列,其中包含我想要的值和列。例如:
I would first create a pandas series that contain the values and columns I want. for example:
new_values= pd.Series(["a", "b"], index=["one", "two"])
然后我将该系列附加到原始数据框:
Then I would append this series to the original dataframe:
df= df.append(new_values, ignore_index=True)
这篇关于在dataframe中循环的空列中添加值的正确方法,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文