Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x {Python}-[Pandas]-如何按小于列名称中的条件对列求和_Python 3.x_Pandas - Fatal编程技术网

Python 3.x {Python}-[Pandas]-如何按小于列名称中的条件对列求和

Python 3.x {Python}-[Pandas]-如何按小于列名称中的条件对列求和,python-3.x,pandas,Python 3.x,Pandas,首先解释数据帧,“0-156”、“156-234”、“234-546”列的值…>76830'是以米为单位的每个距离范围的百分比分布,总计为100%。 列“Cell Name”是指其他列的数据元素,“Distance”列是将触发所需总和的列 我需要对列“0-156”、“156-234”、“234-546”的值求和>76830'小于“距离”(米)列的值 下面是用于测试的创建代码 import pandas as pd # initialize list of lists data = [['T

首先解释数据帧,“0-156”、“156-234”、“234-546”列的值…>76830'是以米为单位的每个距离范围的百分比分布,总计为100%。 列“Cell Name”是指其他列的数据元素,“Distance”列是将触发所需总和的列

我需要对列“0-156”、“156-234”、“234-546”的值求和>76830'小于“距离”(米)列的值

下面是用于测试的创建代码

import pandas as pd 

# initialize list of lists 
data = [['Test1',0.36516562,19.065996,49.15094,24.344206,0.49186087,1.24217,5.2812457,0.05841639,0,0,0,0,158.4122868],
['Test2',0.20406325,10.664485,48.70978,14.885571,0.46103176,8.75815,14.200708,2.1162114,0,0,0,0,192.553074],
['Test3',0.13483211,0.6521175,6.124511,41.61725,45.0036,5.405257,1.0494527,0.012979688,0,0,0,0,1759.480042]
]  

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Cell Name','0-156','156-234','234-546','546-1014','1014-1950','1950-3510','3510-6630','6630-14430','14430-30030','30030-53430','53430-76830','>76830','Distance']) 
应采取的措施示例:
因此,列“距离”的值=158.412286772863必须对值求和据我所知,您希望将一行中的所有百分比值相加,其中列描述的较低值(在“0-156”的情况下为0,在“156-234”的情况下为156,依此类推……)小于“距离”列中的值。 首先,我建议您将类似字符串的列名转换为值,例如:

lowerlimit=df.columns[2]
>>'156-234'
然后只读取字符串直到“-”,并将其设为数字

int(lowerlimit[:lowerlimit.find('-')])
>> 156
您可以在所有列中循环此操作,并为下限创建新行

为了简单起见,我省略了示例中的第一列,并添加了另一行,其中包含每列的下限,您可以如上所述生成该行。那么这个代码就起作用了:

data = [[0,156,234,546,1014,1950,3510,6630,11430,30030,53430,76830,1e-23],[0.36516562,19.065996,49.15094,24.344206,0.49186087,1.24217,5.2812457,0.05841639,0,0,0,0,158.4122868],
[0.20406325,10.664485,48.70978,14.885571,0.46103176,8.75815,14.200708,2.1162114,0,0,0,0,192.553074],
[0.13483211,0.6521175,6.124511,41.61725,45.0036,5.405257,1.0494527,0.012979688,0,0,0,0,1759.480042]
]  

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['0-156','156-234','234-546','546-1014','1014-1950','1950-3510','3510-6630','6630-14430','14430-30030','30030-53430','53430-76830','76830-','Distance'])
df['lastindex']=None
df['sum']=None
在基本上创建了数据框架之后,我添加了两列“lastindex”和“sum”

然后我搜索每行中的最后一个索引,即其下限低于该行中给定的距离(df.iloc[x,-3]);之后,我将对该行中的各个列进行汇总

for i in np.arange(1,len(df)):
    df.at[i,'lastindex']=np.where(df.iloc[0,:-3]<df.iloc[i,-3])[0][-1]
    df.at[i,'sum']=sum(df.iloc[i][0:df.at[i,'lastindex']+1])
np.arange(1,len(df))中的i的


df.at[i,'lastindex']=np.where(df.iloc[0,:-3]据我所知,您希望将一行中的所有百分比值相加,其中列描述的较低值(在'0-156'的情况下为0,在'156-234'的情况下为156,等等)小于距离列中的值。 首先,我建议您将类似字符串的列名转换为值,例如:

lowerlimit=df.columns[2]
>>'156-234'
然后只读取字符串直到“-”,并将其设为数字

int(lowerlimit[:lowerlimit.find('-')])
>> 156
您可以在所有列中循环此操作,并为下限创建新行

为了简单起见,我省略了示例中的第一列,并添加了另一行,其中包含每列的下限,您可以按上述方式生成。然后,此代码可以工作:

data = [[0,156,234,546,1014,1950,3510,6630,11430,30030,53430,76830,1e-23],[0.36516562,19.065996,49.15094,24.344206,0.49186087,1.24217,5.2812457,0.05841639,0,0,0,0,158.4122868],
[0.20406325,10.664485,48.70978,14.885571,0.46103176,8.75815,14.200708,2.1162114,0,0,0,0,192.553074],
[0.13483211,0.6521175,6.124511,41.61725,45.0036,5.405257,1.0494527,0.012979688,0,0,0,0,1759.480042]
]  

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['0-156','156-234','234-546','546-1014','1014-1950','1950-3510','3510-6630','6630-14430','14430-30030','30030-53430','53430-76830','76830-','Distance'])
df['lastindex']=None
df['sum']=None
在基本上创建了数据框架之后,我添加了两列“lastindex”和“sum”

然后我搜索每一行中的最后一个索引,即其下限低于该行中给定的距离(df.iloc[x,-3]);然后我对该行中的各个列求和

for i in np.arange(1,len(df)):
    df.at[i,'lastindex']=np.where(df.iloc[0,:-3]<df.iloc[i,-3])[0][-1]
    df.at[i,'sum']=sum(df.iloc[i][0:df.at[i,'lastindex']+1])
np.arange(1,len(df))中的i的

df.at[i,'lastindex']=np.where(df.iloc[0,:-3]