替换数据帧中的字符串和排序

2024-05-15 10:45:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图替换pandas dataframe列中的字符串,这是成功的,但我缺少其他行,只有两个修改过的字符串及其行保留在DF中。剩下的一件事是根据第三列(Pos1,Pos3,Pos4)的数字对完整的DF进行排序 (参见所需输出)

代码:

df = pd.DataFrame({'1': [5614,4564,3314,3144,1214,4314],
        '2': ['banana','kiwi' ,'salsa','avocado','mix','juice'],
        '3': ['Pos1','Pos1','Pos3','Pos3','Pos1','Pos1']
        })

df = df[4:5].replace('Pos1', 'Pos3') 
# however this doesn't modify the original df but overwrites it with only two rows ( 1214   mix  Pos3, 4314 juice Pos3)


# regarding to locate the integers from 3rd column: 
for indx,row in df.iterrows():
     if row[3].isdigit() == True:
         #... sort_by(row[3]) 
         # but preserve the following order (check desired output)

Dataframe

    1       2       3
0   5614    banana  Pos1
1   4564    kiwi    Pos4
2   3314    salsa   Pos3
3   3144    avocado Pos3
4   1214    mix     Pos1
5   4314    juice   Pos1


Desired output:

    1       2       3
0   5614    banana  Pos1
1   3314    salsa   Pos3
2   3144    avocado Pos3
3   1214    mix     Pos3
4   4314    juice   Pos3
5   4564    kiwi    Pos4

编辑:顺序/排序问题(不保留“内部”/“Pos1或Pos3”组中的确切顺序,但它是分散的)

图片: enter image description here

如果你看这张图片,前10个项目的顺序应该与它从一开始的顺序完全相同,但即使是这些项目也没有顺序:它是无序的。它应该是:0,1,2,3,4,5,6,7。。10但不是0,9,7,6,5,8,3,2,1(这些都是“位置1”)


Tags: the字符串df顺序juicerowbananamix
2条回答

这回答了你的问题吗

df = pd.DataFrame({'1': [5614,4564,3314,3144,1214,4314],
        '2': ['banana','kiwi' ,'salsa','avocado','mix','juice'],
        '3': ['Pos1','Pos4','Pos3','Pos3','Pos1','Pos1']
        })
# replace strings only in column 3
df.loc[4:, '3'] = df.loc[ 4:,'3'].replace('Pos1', 'Pos3')
# sort values by column '3'
df = df.sort_values('3')
df
>>>
      1        2     3
0  5614   banana  Pos1
2  3314    salsa  Pos3
3  3144  avocado  Pos3
4  1214      mix  Pos3
5  4314    juice  Pos3
1  4564     kiwi  Pos4

这将替换“3”中大于4的所有索引的所有字符串。稍后,它将按此列排序(无需重新编制索引)

如果希望索引保持不变,则必须使用df = df.sort_values('3', ignore_index=True)进行排序

更新

如果您想在索引大于3后对数据帧进行排序,则必须先进行切片,然后进行排序并替换值。可以像这样

df.loc[3:] = df.loc[3:].sort_values('3').set_index(df.loc[3:].index)

调用set_index是很重要的,因为如果不这样做,右侧将再次从0开始,并且将用NaN值填充数据帧

替换行应为:

df[4:5] = df[4:5].replace('Pos1', 'Pos3') 

原始代码的问题是它只传递[4:5]给df

相关问题 更多 >