替换大 pandas 数据框中的行 [英] replace rows in a pandas data frame

查看:126
本文介绍了替换大 pandas 数据框中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个空的数据框开始,然后每次添加一行。
我甚至可以从0数据框开始 data = pd.DataFrame(np.zeros(shape =(10,2)),column = [a,b]) ,然后每次更换一行。

I want to start with an empty data frame and then add to it one row each time. I can even start with a 0 data frame data=pd.DataFrame(np.zeros(shape=(10,2)),column=["a","b"]) and then replace one line each time.

我该怎么做?

推荐答案

$ c> .loc 用于基于标签的选择,重要的是您了解如何正确切片: http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-label ,并了解为什么你应该避免链接作业: http://pandas.pydata.org /pandas-docs/stable/indexing.html#indexing-view-versus-copy

Use .loc for label based selection, it is important you understand how to slice properly: http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-label and understand why you should avoid chained assignment: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [14]:

data=pd.DataFrame(np.zeros(shape=(10,2)),columns=["a","b"])
data
Out[14]:
   a  b
0  0  0
1  0  0
2  0  0
3  0  0
4  0  0
5  0  0
6  0  0
7  0  0
8  0  0
9  0  0

[10 rows x 2 columns]
In [15]:

data.loc[2:2,'a':'b']=5,6
data
Out[15]:
   a  b
0  0  0
1  0  0
2  5  6
3  0  0
4  0  0
5  0  0
6  0  0
7  0  0
8  0  0
9  0  0

[10 rows x 2 columns]

这篇关于替换大 pandas 数据框中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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