gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:58:23 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com