gpt4 book ai didi

python-3.x - 根据条件删除 DataFrame 中的元素

转载 作者:行者123 更新时间:2023-12-03 16:40:20 25 4
gpt4 key购买 nike

我的 DataFrame 正在查看这个:

indeed.fr
11.41%
career2.successfactors.eu
8.53%
37.16%
pracuj.pl
7.40%
80.42%
corporate.danone.com.br
6.64%
indeed.com.br
4.68%
61.73%

因此我只想保留前 % 如下:

indeed.fr
11.41%
career2.successfactors.eu
8.53%
pracuj.pl
7.40%
corporate.danone.com.br
6.64%
indeed.com.br
4.68%

所有行都是字符串等等我不知道我们是否可以在前一行包含 % 的情况下删除行?

有什么想法吗?

感谢您的宝贵时间!

mydata =['indeed.fr','11.41%','career2.successfactors.eu','8.53%','37.16%','pracuj.pl','7.40%','80.42%','corporate.danone.com.br','6.64%','indeed.com.br','4.68%','61.73%']
df=pd.DataFrame(mydata)

最后,我想要这个:My need

最佳答案

mydata =['indeed.fr','11.41%','career2.successfactors.eu','8.53%','37.16%','pracuj.pl','7.40%','80.42%','corporate.danone.com.br','6.64%','indeed.com.br','4.68%','61.73%']

df = pd.DataFrame(mydata)

是您创建的示例。

解决方法如下

rowList = []
row = []

#Variable to keep track of the number of times I see the percentage value
percentVal = 0

for i in df.index:


if(df.at[i, 0][0] not in set('0123456789')):


row.append(df.at[i, 0])

percentVal = 0

else:


percentVal += 1

if(percentVal != 2):
row.append(df.at[i, 0])
rowList.append(row)
row = []

else:
#If percentVal == 2, that means, I have seen my second percentage value and I'm going to skip it.
print("Skipping {}".format(df.at[i, 0]))
row = []



yourSol = pd.DataFrame(rowList)
yourSol.columns = ['Incoming Referal Sources', 'Value (%)']

print(yourSol)

关于python-3.x - 根据条件删除 DataFrame 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50904164/

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