Here is an illustration of what I'm trying to accomplish 有人可以建议一些代码,这些代码可以让我根据不同的标准找到数据框中某些数字的中位数。
然后,我想将这些中值添加到numlist中每个i值的数据框中。
我到目前为止所使用的代码,但是针对不同的.loc标准重复了两次。 如果可能的话,我想将结果中值添加到数据框中,而不添加列表中的所有不必要的步骤。
此外,当我连接数据帧时,numlist中的i值会在结果中出现两次。 numlist中的那些i值(附加到列表n)应该是第一列中添加了不同中值的值。
import pandas as pd
import numpy as np
testdata= pd.DataFrame(pd.read_csv('C:\Users\Desktop\Data_Sample.csv'))
numberlist= testdata.loc[:,["ML#"]]# list of ML numbers
numlista=numberlist['ML#'].str[:5] # sliced the ML numbers
numlist=list(set(numlista)) #leaves original sliced values only, removes duplicates
testdata['e']=testdata["ML#"].str[:5] #added a column to the original testdata with a sliced ML# field
y=[]; n=[]# will be making some lists to later add data to a pandas frame
for i in numlist: # for each original sliced ML number (subs)
a=testdata.loc[(testdata["ABC"]==1) & (testdata["Price"]>0) &(testdata["Price"]<1000000) & (testdata['e']==str(i))] #sets a to be a list of data
#that meets criteria (was (testdata["ML#"].str[:5]==str(i)))
b= np.mean(a.loc[:,["Price"]]) # found mean of the Prices of entries that met above criteria
c=b.tolist() # got just the values of said means
y.append(str(c)) #made list for DF
n.append(str(i)) #made list for DF
df1=pd.DataFrame({'List of serial numbers': n,'1=ABC-Mean found': y}) #
n=[]; y=[] # had to empty these to add new values
#Second loop, building the list of median values for each i under new criteria
for i in numlist: # for each original sliced ML number (subs)
a=testdata.loc[(testdata["ABC"]==2) & (testdata["Price"]>=1000000) &(testdata["Price"]<2000000) & (testdata['e']==str(i))] #sets a to be a list of data
#that meets criteria (was (testdata["ML#"].str[:5]==str(i)))
b= np.mean(a.loc[:,["Price"]]) # found mean of the Sale Prices of entries that met above criteria
c=b.tolist() # got just the values of said means
y.append(str(c)) #made list for DF
n.append(str(i)) #made list for DF
df2=pd.DataFrame({'List of serial numbers': n,'2=ABC-Mean found': y})
result = pd.concat([df1, df2], axis=1, join='inner') #need the means for testdata["ABC"]==2 and testdata["ABC"]==1 to appear alongside the list of original i's in list n.
谢谢