我正试图让我的数据帧在列中插入一个特定的值。所需的值将是“返回天气”,其中“城市”和“天”都与指定的值匹配。以下是一个玩具示例:
index City Weather Day Return City Return Weather
0 New York 65 3 Baltimore
1 Atlanta 77 6 Chicago
2 Baltimore 68 9 Orlando
3 Chicago 62 13 Boston
4 Boston 57 14 Atlanta
5 Orlando 88 19 New York
6 Baltimore 59 3 Chicago
7 Chicago 66 6 Atlanta
下面是我正在尝试的代码:
for i in df.index:
day = df.loc[i, 'Day']
rtn_city = df.loc[i, 'Return City']
df.loc[i, 'Return Weather'] = df.loc[(df['City'] == rtn_city) & (df['Day'] == day), 'Weather'].values
但是我得到:
ValueError: Must have equal len keys and value when setting with an iterable
如果我把最后一行设置为打印。。。你知道吗
results = df.loc[(df['City'] == rtn_city) & (df['Day'] == day), 'Temp'].values
print(results)
我得到了正确的答案,但它们似乎是列表:
[59]
[66]
闻起来像是我的问题。有什么想法吗?你知道吗
有了给定的索引集,您可以使用.loc[]直接为不同的“姓氏”值选择行—根据您的情况,可以是单个的,也可以是多个。。你知道吗
或者尝试使用@Sai Kumar作为特定索引。。你知道吗
相关问题 更多 >
编程相关推荐