python - 使用字符串的出现过滤数据框内容

标签 python pandas dataframe

I have a dataframe of the following type,
              Input         Output         Output SE
  0           Rat           Cat               Mat
  1           rat           cat               mat
  2           0             4.8               0.255
  3           3             7.2               0.32
  4           Bat           Cat               Sat
  5           bat           cat               sat
  6           0             1.8               0.275
  7           3             1.7               0.745

这些值是从 Excel 中读取的。

我想把这些内容分开如下

df1=
0            Rat            Cat               Mat
1            rat            cat               mat
2            0              4.8               0.255
3            3              7.2               0.32


df2=

   0         Bat              Cat               Sat
   1         bat              cat               sat
   2         0                1.8               0.275
   3         3                1.7               0.745

我目前正在使用 iloc:

df1 = df.iloc[0:3]
df2 = df.iloc[4:8]

还有其他方法吗? 我有一个非常大的具有相同模式的数据帧,我想在出现两行字符串时拆分数据帧。

编辑:输入数据框重置

最佳答案

尝试通过//创建一个新 key >

s1=df.Input.str.isdigit()

for x , y in df.groupby((~s1&s1.shift().fillna(True)).cumsum()):
    print(x,y)


1   Input Output Output SE
0   Rat    Cat       Mat
1   rat    cat       mat
2     0    4.8     0.255
3     3    7.2      0.32
2   Input Output Output SE
4   Bat    Cat       Sat
5   bat    cat       sat
6     0    1.8     0.275
7     3    1.7     0.745

d={x : y for x , y in df.groupby((~s1&s1.shift().fillna(True)).cumsum())}

关于python - 使用字符串的出现过滤数据框内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56973543/

相关文章:

python - 在 python 中创建 GraphQL 查询

python Pandas : split comma-separated column into new columns - one per value

python - Excel 使用 Python Pandas 合并 2 个工作表中的单元格

python - Pandas:当大小不相等时,如何将 Python 列表中的值分配给数据框中的列

python - 使用映射函数在 pandas 列中绘制直方图

python - 循环遍历具有相同子字符串的文件

python - OS X 终端中 Python 解释器中的制表符补全

python - Ubuntu 14.04 LTS 中的 PyObject Pycharm 设置失败

pandas - 在 Pandas Dataframe 中拆分列标题并复制行值

python - 用 Pandas 数据框中另一列中的值替换空列表