【问题标题】:insert an indexed new row based on condition根据条件插入索引的新行
【发布时间】:2018-11-04 12:45:07
【问题描述】:

我有一个带有命名索引和两列的 pandas df,如图所示。

df = pd.DataFrame(columns=['system_call','frequency','file_frequency'])
df.set_index('system_call', inplace=True)

如果满足 if-else 条件,我想添加一个新行。 我试过df.loc[-1]=[words[0],words[1],1],但我认为它可能适用于未命名的索引。

预期输出-

system_call   frequency file_frequency

madvise          300    3
write            277    2
read             23     5
ioctl            45     4
getuid           78     2
epoll_pwait      12     1
futex            13     6

可以看出现在添加了最后一行

【问题讨论】:

  • try df.iloc[-1]=[words[0],words[1],1] loc 用于基于标签的标签 iloc 用于整数位置
  • @EdChum 但我的数据框有两列,其中包含单词 [1] 和 1,索引是单词 [0]

标签: python pandas dataframe


【解决方案1】:

如果futex 不存在于index 中,我相信需要:

df.loc['futex']=[13,6]

print (df)
             frequency  file_frequency
system_call                           
madvise            300               3
write              277               2
read                23               5
ioctl               45               4
getuid              78               2
epoll_pwait         12               1
futex               13               6

如果存在,行被重写:

df.loc['madvise']=[130,100]
print (df)
             frequency  file_frequency
system_call                           
madvise            130             100
write              277               2
read                23               5
ioctl               45               4
getuid              78               2
epoll_pwait         12               1

如果需要始终添加新行,请使用 append by Series 将索引作为列名,name 用于新索引值:

df = df.append(pd.Series([13,6], name='futex', index=df.columns))
print (df)
             frequency  file_frequency
system_call                           
madvise            300               3
write              277               2
read                23               5
ioctl               45               4
getuid              78               2
epoll_pwait         12               1
futex               13               6

df = df.append(pd.Series([130,100], name='madvise', index=df.columns))
print (df)
             frequency  file_frequency
system_call                           
madvise            300               3
write              277               2
read                23               5
ioctl               45               4
getuid              78               2
epoll_pwait         12               1
madvise            130             100

【讨论】:

  • 如果这是索引“futex”的第一次输入,这会起作用吗?
  • 是的,如果第一次futexiot添加新行,如果存在,行将被重写。
  • 非常感谢!...您也知道如何更新 pandas 中的行吗?我做了 df.loc['madvise',130]+=int(words[1]) 但出错了
  • @ubuntu_noob - 什么样的错误? df.loc['madvise',130] = df.loc['madvise',130] + 10 工作?或df.loc['madvise',130] += 10
  • 他们俩 df.loc['madvise',130] = df.loc['madvise',130] + 10 和 df.loc['madvise',130] += 10 都给error..which is TypeError: cannot do label indexing on with these indexers [32] of
【解决方案2】:

如果要在数据框中添加具有指定索引值的行

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
print(df)

df.loc["21"] = {"A":3,"B":3}
print(df)

具有列名和值的字典。 试试这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2021-05-11
    • 2022-01-19
    • 2023-04-06
    • 2021-05-21
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多