使用循环将数据帧输出到csv(Outputting dataframes to csv using a loop)

编程入门 行业动态 更新时间:2024-10-28 14:36:16
使用循环将数据帧输出到csv(Outputting dataframes to csv using a loop)

我有一个从数据框构建的组,并试图理解为什么这不起作用:

for i in range(1,12): out1=df.ix['group%s'% i] out1.to_csv('group%s.csv' % i) out1.pl.describe()

group_i的一个示例是:(a,b包含浮点数)

group1=df["ptdelta"][a & b] a=df["ptdelta"]>=0 b=df["ptdelta"]<5

回溯给出了KeyError:group1(意思是第一次尝试)

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-87-11399ea315df> in <module>() 6 ''' 7 for i in range(1,12): ----> 8 out1=df.ix['group%s'% i] 9 out1.to_csv('group%s.csv' %i) 10 out1.pl.describe() C:\Python27\lib\site-packages\pandas\core\indexing.pyc in __getitem__(self, key) 32 return self._getitem_tuple(key) 33 else: ---> 34 return self._getitem_axis(key, axis=0) 35 36 def _get_label(self, label, axis=0): C:\Python27\lib\site-packages\pandas\core\indexing.pyc in _getitem_axis(self, key, axis) 343 return self._get_loc(key, axis=0) 344 --> 345 return self._get_label(idx, axis=0) 346 else: 347 labels = self.obj._get_axis(axis)

I have groups constructed from a dataframe & trying to understand why this doesn't work:

for i in range(1,12): out1=df.ix['group%s'% i] out1.to_csv('group%s.csv' % i) out1.pl.describe()

An example of a group_i is: (a,b contain floats)

group1=df["ptdelta"][a & b] a=df["ptdelta"]>=0 b=df["ptdelta"]<5

The traceback gives KeyError: group1 (meaning the first try)

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-87-11399ea315df> in <module>() 6 ''' 7 for i in range(1,12): ----> 8 out1=df.ix['group%s'% i] 9 out1.to_csv('group%s.csv' %i) 10 out1.pl.describe() C:\Python27\lib\site-packages\pandas\core\indexing.pyc in __getitem__(self, key) 32 return self._getitem_tuple(key) 33 else: ---> 34 return self._getitem_axis(key, axis=0) 35 36 def _get_label(self, label, axis=0): C:\Python27\lib\site-packages\pandas\core\indexing.pyc in _getitem_axis(self, key, axis) 343 return self._get_loc(key, axis=0) 344 --> 345 return self._get_label(idx, axis=0) 346 else: 347 labels = self.obj._get_axis(axis)

最满意答案

您正尝试使用字符串值“group”进行索引,该值不是数据帧中的有效索引。 如果列表中已有多个组,并且每个组都是具有正确大小索引的系列,则可以尝试:

i = 1 for group in groups: out1=df.ix[group] out1.to_csv('group%s.csv' % i) out1.pl.describe() i += 1

You are trying to index with the string value 'group' which is not a valid index in your dataframe. If you have several groups already in a list and each group is a series with the correct size index, you can try:

i = 1 for group in groups: out1=df.ix[group] out1.to_csv('group%s.csv' % i) out1.pl.describe() i += 1

更多推荐

本文发布于:2023-08-05 09:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   csv   Outputting   dataframes   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!