大熊猫 - 不在索引中(pandas - not in index)

我正在尝试替换列'A'中的nan值(第一行出现在第9行),前一个值是前一行

y = len(df)

for i in range(1,y):

if (np.isnan(df.ix[i,'A'])):

df.ix[i,'A'] = dfH.ix[i-1,'A']

给出错误

KeyError: "[9 'A'] not in index"

但是第9行A列显然在我的dataFrame中。 这里发生了什么?

I'm trying to replace the nan values in column 'A' (for which the first one appears in row 9) with the previous value is the row before

y = len(df)

for i in range(1,y):

if (np.isnan(df.ix[i,'A'])):

df.ix[i,'A'] = dfH.ix[i-1,'A']

gives the error

KeyError: "[9 'A'] not in index"

but row 9 column A is clearly in my dataFrame. What is going on here?

原文:https://stackoverflow.com/questions/39955687

更新时间:2019-09-04 16:02

最满意答案

你可以使用fillna和ffill方法:

df['A'].fillna(method='ffill', inplace = True)

You could use fillna with ffill method:

df['A'].fillna(method='ffill', inplace = True)

2016-10-10

相关问答

>>> myseries[myseries == 7]

3 7

dtype: int64

>>> myseries[myseries == 7].index[0]

3

虽然我承认应该有一个更好的方法来做到这一点,但是这至少避免了迭代和循环遍历对象并将其移动到C级。 >>> myseries[myseries == 7]

3 7

dtype: int64

>>> myseries[myseries == 7].index[0]

3

Though I admit that there

...

首先通过左连接合并两个DataFrames的索引: pd.merge(staff_df, student_df, how='left', left_index=True, right_index=True)

第二个,并在两个DataFrames按列Name简化第三个左连接: pd.merge(staff_df, student_df, how='left', left_on='Name', right_on='Name')

pd.merge(staff_df, student_df, how='

...

In [5]: a.reset_index().merge(b, how="left").set_index('index')

Out[5]:

col1 to_merge_on col2

index

a 1 1 1

b 2 3 2

c 3 4 NaN

In [5]: a.reset_index().merge(b, how="left").set

...

当然可以使用.get_loc() : In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})

In [46]: df.columns

Out[46]: Index([apple, orange, pear], dtype=object)

In [47]: df.columns.get_loc("pear")

Out[47]: 2

虽然老实说,我不经常需要这个。 通常按名称访问我想要的( d

...

请参阅docs: http : //pandas.pydata.org/pandas-docs/stable/dsintro.html#column-selection-addition-deletion 使用idx = 0将插入开头 df.insert(idx, col_name, value)

see docs: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.insert.html using l

...

你可以使用fillna和ffill方法: df['A'].fillna(method='ffill', inplace = True)

You could use fillna with ffill method: df['A'].fillna(method='ffill', inplace = True)

这是一个非常紧凑的方式。 为了简单起见,我用排名写了'得分'值,但如果你愿意的话,你可以保留原来的得分(这只是更冗长的一点)。 df['score'] = df.groupby('group')['score'].rank()

df.set_index(['group','score']).unstack()

subgroup

score 1 2 3

group

A B C A

B

...

使用groupby和参数levels对数据进行分组,然后使用mean和std 。 如果您希望将平均值作为现有数据框中的新列,请使用transform返回与您的df具有相同索引的Series: grouped = df.groupby(level = ['Country','State', 'City'])

df['Mean'] = grouped['price_observation'].transform('mean')

df['Std'] = grouped['price_observatio

...

这就是map的用途。 只做u = s.map(t) 。 That's what map is for. Just do u = s.map(t).

你想要满足什么条件? import pandas as pd

df=pd.DataFrame([['This is also a interesting topic',2],['

the valley of flowers ...',1],['found in the hilly terrain',5],

['we must preserve it ',6]],columns=['description','count'])

print(df)

...

pandas不显示index_大熊猫 - 不在索引中(pandas - not in index)相关推荐

  1. python pandas 读取excel 去重某一列_Python中Pandas读取修改excel操作攻略(代码示例)...

    本篇文章给大家带来的内容是关于Python中Pandas读取修改excel操作攻略(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 环境:python 3.6.8 以某米赛 ...

  2. pandas不显示index_pandas层级索引

    层级索引(hierarchical indexing) 下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list, 第一个子list是外层索引,第二个list是内层索引 ...

  3. pandas使用query函数查询指定日期索引对应的dataframe数据行(select rows using a single date in dataframe)

    pandas使用query函数查询指定日期索引对应的dataframe数据行(select rows using a single date in dataframe) 目录 pandas使用qu

  4. pandas使用np.where函数计算返回dataframe中指定数据列包含缺失值的行索引列表list

    pandas使用np.where函数计算返回dataframe中指定数据列包含缺失值的行索引列表list(index of rows with missing values in dataframe ...

  5. pandas自定义设置dataframe每个索引的标签、自定义设置索引的列名称(customize index name and index label)

    pandas自定义设置dataframe每个索引的标签.自定义设置索引的列名称(customize index name and index label) 目录

  6. pandas使用query函数查询指定日期索引之间对应的dataframe数据行(select rows date index between a certain date interval)

    pandas使用query函数查询指定日期索引之间对应的dataframe数据行(select rows where date index between a certain date interva ...

  7. Pandas中xs()函数索引复合索引数据的不同切面数据(索引复合索引中需要的数据):索引列复合索引中的一个切面、索引行复合索引中的一个切面

    Pandas中xs()函数索引复合索引数据的不同切面数据(索引复合索引中需要的数据):索引列复合索引中的一个切面.索引行复合索引中的一个切面 目录

  8. pandas使用argmax函数返回给定series对象中最大值(max、maximum)的行索引实战

    pandas使用argmax函数返回给定series对象中最大值(max.maximum)的行索引实战 目录 pandas使用argmax函数返回给定series对象中最大值的行索引实战

  9. pandas(一)-- Series创建及索引

    pandas Series 构建 通过数组的方法 ar = [90.0,92.0,89.0,65.0] s = pd.Series(ar,index=['Jack','Marry','Tom','Za ...

最新文章

  1. R语言dplyr包连接多个dataframe实战:使用left_join
  2. 局域网读取文件_教你windows局域网如何设置共享文件
  3. [BUUCTF-pwn]——[OGeek2019]babyrop
  4. 全景图像拼接——图像融合
  5. 单位矩阵的逆| 使用Python的线性代数
  6. QQ浏览器怎么设置主页 QQ浏览器设置默认主页教程
  7. 如何查看linux 日志 磁盘使用情况?
  8. 浅谈软件开发过程及项目管理
  9. 更新智能开发研发进度
  10. cobbler的搭建
  11. python中格式化输出_python如何实现格式化输出
  12. 南阳oj-----找球号(一)(set)
  13. Wpf初学 ---03设计一个优美的注册登录界面(连接数据库)
  14. linux系统中 为mysql还原数据库_linux中mysql还原数据库命令
  15. 流量分析和强制执行ntopng
  16. DCOS到底是啥?看完这篇你就懂了
  17. MC皮肤站和外置登陆教程
  18. 为什么打不开jar文件?
  19. Typora的光标状态切换
  20. 一个简单的Java程序:My first Java!

热门文章

  1. IntelliJ IDEA 的 Code Coverage 测试
  2. mysql 查询 写入 性能极致优化 3000万数据3分钟 深层次优化大数据量
  3. poi-tl导出word;自定义列表序号和表格宽度,表格合并,自定义标题,更新目录
  4. 易买网更多新闻代码_《女神异闻录5》新网站源代码内藏玄机 4月25日有更多情报...
  5. 强化学习萨顿和巴托部分习题
  6. Navicat Premium导入Excel提示无法打开Excel文件
  7. 朋友圈文案_隐私条款
  8. 关于Qt各个版本的下载地址
  9. 论文笔记—A Review of Visual-LiDAR Fusion based Simultaneous Localization and Mapping
  10. 电信大数据治理与质量管理