gpt4 book ai didi

Python - 从数据框中删除行( Pandas )

转载 作者:行者123 更新时间:2023-12-01 23:22:40 25 4
gpt4 key购买 nike

我正在尝试从数据框中删除某些信息,但“删除命令”(.drop) 无法正常工作,有人知道吗?

我的代码:

    import pandas as pd

def join():

open_momox_xlsx = "momox_ergebnisse.xlsx"
open_rebuy_xlsx = "rebuy_ergebnisse.xlsx"

rebuy_xlsx = pd.read_excel(open_rebuy_xlsx)
momox_xlsx = pd.read_excel(open_momox_xlsx)

rebuy_data = rebuy_xlsx[['ReBuy']]
isbn_data = rebuy_xlsx[['ISBN']]
momox_data = momox_xlsx[['Momox']]

dataframe = pd.DataFrame =({'ISBN': isbn_data, 'Rebuy': rebuy_data, 'Momox': momox_data})
data = pd.concat(dataframe,axis=1, ignore_index=True)

c=0
#print(data[0])
while c < len(data):

if data[1][c] and data[2][c] == '///':
data.drop(index=c)
elif data[1][c] and data[2][c] < '1':
data.drop(index=c)
elif data[1][c] or data[2][c] < '1' and data[1][c] or data[2][c] == '///' :
data.drop(index=c)
c=c+1
print(data)

输出:

                0      1      2
0 9783630876672 12,35 2.62
1 9783423282789 11,67 6.07
2 9783833879500 17,25 12.40
3 9783898798822 6,91 1.16
4 9783453281417 12,93 2.84
5 9783630876672 12,35 4.08
6 9783423282789 11,67 6.07
7 9783833879500 17,25 9.94
8 9783898798822 6,91 2.96
9 9783453281417 12,93 2.68
10 3927905909 /// ///
11 3872948210 /// 0.15
12 9783293003781 /// 0.15
13 9783423246842 /// ///
14 9783423247146 /// ///
15 9783423246934 /// ///
16 387294116x /// ///
17 9783935597456 0,16 0.15
18 9783423204545 /// ///

想要的输出:

                0      1      2
0 9783630876672 12,35 2.62
1 9783423282789 11,67 6.07
2 9783833879500 17,25 12.40
3 9783898798822 6,91 1.16
4 9783453281417 12,93 2.84
5 9783630876672 12,35 4.08
6 9783423282789 11,67 6.07
7 9783833879500 17,25 9.94
8 9783898798822 6,91 2.96
9 9783453281417 12,93 2.68

if 语句似乎工作正常,但 data.drop 没有做它应该做的事..

最佳答案

您应该将 inplace=True 添加到 drop 函数。

关于Python - 从数据框中删除行( Pandas ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67782727/

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