gpt4 book ai didi

python - 对象类型没有名为 length 的轴,尝试根据数据帧的长度进行排序并将其屏蔽到第二个数据帧

转载 作者:行者123 更新时间:2023-12-01 06:25:34 24 4
gpt4 key购买 nike

我有 2 个数据帧,我想按我使用 str.len() 的字符串长度对第一个数据帧的值进行排序,然后根据该数据帧的索引对第二个数据帧进行排序我正在尝试使用 pandas.masking 第二个数据框,但给了我错误任何建议?
两个数据帧的索引匹配。我的代码

wdata = pd.read_csv(fileinput, nrows=0).columns[0]
skip = int(wdata.count(' ') == 0)
wdata = pd.read_csv(fileinput, names=['sentences'], skiprows=skip)

length= wdata['sentences'].str.len().sort_values()
print(length)
sort= wdata['sentences'].sort_values('length', ascending=True, inplace=True).any(axis=1)
df=sort

print(df)

df2 = pd.read_csv(fileinput, nrows=0).columns[0]
skip = int(df2.count(' ') == 0)
df2 = pd.read_csv(fileinput, names=['sentences'], skiprows=skip)
newdata2 = df2[df2.sort(df.index)]
print(newdata2)

----------------------
#first dataframe example
----------------------
#how are you
#I want to die
#I was home
#I went to sleep at work
#he have a bad reputation
#it was me who went to him
#have good sleep home
#yes
#I'm good

----------------------
#second dataframe example
----------------------
#halaw kuy bashii
#damawe bmrm
#la malawa bum
#la esh nustm
#aw kabraya bash nya
#awa mn bum chum bo lay
#xaweki xosh basar bba la malawa
#bale
#mn bashm

我期望的输出是

enter image description here

enter image description here

我遇到的错误

raise ValueError("No axis named {0} for object type {1}".format(axis, cls))
ValueError: No axis named length for object type <class 'pandas.core.series.Series'>

我做错了什么,请有解决办法吗?

最佳答案

首次使用Series.argsort对于排序值的位置,然后传递到 DataFrame.iloc :

idx = wdata['sentences'].str.len().argsort()
df = wdata.iloc[idx]
print (df)
sentences
7 yes
8 Im good
2 I was home
0 how are you
1 I want to die
6 have good sleep home
3 I went to sleep at work
4 he have a bad reputation
5 it was me who went to him

如果要选择一列进行系列:

sentences = df['sentences']

对于第二次使用相同,如果相同的索引值如wdata:

newdata2 = df2.iloc[idx]

关于python - 对象类型没有名为 length 的轴,尝试根据数据帧的长度进行排序并将其屏蔽到第二个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60164059/

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