附加到python/pandas中的Series无效 [英] Append to Series in python/pandas not working

查看:125
本文介绍了附加到python/pandas中的Series无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将值附加到通过找到nth和nth + 1元素之间的差异而获得的pandas系列中:

I am trying to append values to a pandas Series obtained by finding the difference between the nth and nth + 1 element:

q = pd.Series([])

while i < len(other array):
    diff = some int value
    a = pd.Series([diff], ignore_index=True)
    q.append(a)
    i+=1

我得到的输出是:

Series([], dtype: float64)

为什么我没有获得所有附加值组成的数组?

Why am I not getting an array with all the appended values?

-

P.S.这是一个数据科学问题,我必须通过搜索数据框来查找县数最多的州.我使用一种状态结束而下一个状态开始的索引值(我用来查找差异的数组中的值)来确定该状态下有多少个县.如果有人比我更好地解决了这个问题,请告诉我!

P.S. This is a data science question where I have to find state with the most counties by searching through a dataframe. I am using the index values where one state ends and the next one begins (the values in the array that I am using to find the difference) to determine how many counties are in that state. If anyone knows how to solve this problem better than I am above, please let me know!

推荐答案

append方法无法就地工作.而是返回一个新的Series对象.所以应该是:

The append method doesn't work in-place. Instead, it returns a new Series object. So it should be:

q = q.append(a)

希望有帮助!

这篇关于附加到python/pandas中的Series无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆