Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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 如何在单个键的内部值中正确使用apply和int_Python_Pandas_String_Dataframe_Integer - Fatal编程技术网

Python 如何在单个键的内部值中正确使用apply和int

Python 如何在单个键的内部值中正确使用apply和int,python,pandas,string,dataframe,integer,Python,Pandas,String,Dataframe,Integer,我有一个基本问题:我正在使用以下脚本: import pandas as pd from collections import OrderedDict df = pd.DataFrame({'ID' : ['ID1', 'ID1', "ID1","ID2","ID2"], "pdb" : ["a", "b", "c","d",&quo

我有一个基本问题:我正在使用以下脚本:

import pandas as pd
from collections import OrderedDict


df = pd.DataFrame({'ID' : ['ID1', 'ID1', "ID1","ID2","ID2"], "pdb" : ["a", "b", "c","d","e"], "beg": [1, 3, 40,111,100], "end" : [11, 12, 50,115,110]})
df2 = pd.DataFrame

for index, row in df.iterrows(): 
    df['var1'] = df.apply(lambda x : " ".join(list(map(str,range(x['beg'],x['end']+1)))),axis=1)
    df2 = df.groupby(["ID"], sort=False)['var1']
    .apply(lambda x : (' '.join(x.astype(str)))).reset_index(name='var1')  
    df2['var1'] = (df2['var1'].str.split().apply(lambda x: (OrderedDict.fromkeys(x).keys()))
    .str.join(' '))
    df2["var1"] = df2["var1"].map(lambda x: int(x))    
    df2["var2"] = (df2["var1"].str.split().apply(lambda x: sorted(x)).str.join(" "))
我在尝试将一串数字转换为整数时遇到了这个错误,因此可以正确排序:(从这一行:
df2[“var1”]=df2[“var1”].map(lambda x:int(x)

有没有合适的方法?
提前感谢。

我认为您可以创建新的列
r
,其中包含范围,然后排序、删除重复项并转换为字符串,然后通过
ID
加入每个组:

df['r'] = df.apply(lambda x: range(x['beg'],x['end']+1), axis=1)
df2 = df.explode('r').drop_duplicates(['ID','r']).sort_values(['ID','r'])
df2['r'] = df2['r'].astype(str)
df2 = df2.groupby('ID')['r'].agg(' '.join).reset_index()
print (df2)
    ID                                                  r
0  ID1  1 2 3 4 5 6 7 8 9 10 11 12 40 41 42 43 44 45 4...
1  ID2  100 101 102 103 104 105 106 107 108 109 110 11...
在您的解决方案中,可以将每个值映射到int,然后排序,最后映射到字符串,如:

df2["var2"] = df2["var1"].str.split().apply(lambda x: ' '.join(list(map(str,sorted(map(int,x))))))

看起来很好!干杯!
df2["var2"] = df2["var1"].str.split().apply(lambda x: ' '.join(list(map(str,sorted(map(int,x))))))