对象类型没有名为长度的轴,尝试根据数据帧的长度进行排序并将其屏蔽到第二个数据帧
No axis named length for object type, trying to sort based on length of dataframe and mask it to the second datafram
我有 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
我期望的输出是
我遇到的错误
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
for positions of sorted values, so then pass to 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
如果想要 select 一列到 Series:
sentences = df['sentences']
对于第二次使用相同,如果相同的索引值如 wdata
:
newdata2 = df2.iloc[idx]
我有 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
我期望的输出是
我遇到的错误
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
for positions of sorted values, so then pass to 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
如果想要 select 一列到 Series:
sentences = df['sentences']
对于第二次使用相同,如果相同的索引值如 wdata
:
newdata2 = df2.iloc[idx]