我有一个看起来像这样的DataFrame:
columns = ['C24H31O8', 'C23H27O9', 'C22H23O10', 'C21H19O11', 'C20H15O12','C19H11O13']
values= [[ 0., 37., 74., 111., 148., 185.],
[ -37., 0., 37., 74., 111., 148.],
[ -74., -37., 0., 37., 74., 111.],
[-111., -74., -37., 0., 37., 74.],
[-148., -111., -74., -37., 0., 37.],
[-185., -148., -111., -74., -37., 0.]]
您可以使用轻松地重新创建我的数据框
df = pd.DataFrame(data=values, index=columns)
我要遍历数据框,如下所示:
进行迭代时,在C24H31O8行中的第1列中找到了37。现在,转到第1行,然后再次进行迭代。如果您再次找到数字37(在这种情况下,您在第三列中找到了37),请转到第三行,然后再次搜索37,依此类推。
我想要的输出应该是一条链:
37 : C24H31O9 --> C23H27O9 --> C22H23O10 --> C21H19O11 ...
(为每个值做些事情)
答案 0 :(得分:0)
如果我正确理解了这个问题,这应该可以工作:
columns= pd.Index(['C24H31O8', 'C23H27O9', 'C22H23O10', 'C21H19O11', 'C20H15O12','C19H11O13'],dtype='object')
values=np.array([[ 0., 37., 74., 111., 148., 185.],
[ -37., 0., 37., 74., 111., 148.],
[ -74., -37., 0., 37., 74., 111.],
[-111., -74., -37., 0., 37., 74.],
[-148., -111., -74., -37., 0., 37.],
[-185., -148., -111., -74., -37., 0.]])
df = pd.DataFrame(values, columns)
def build_string(df, i):
row = 0
chain = df.index[0]
while True:
try:
row = df.iloc[row][df.iloc[row] == i].index[0]
chain += f"--> {df.index[row]}"
except IndexError:
break
return chain
输出:
>>> build_string(df, 37)
'C24H31O8--> C23H27O9--> C22H23O10--> C21H19O11--> C20H15O12--> C19H11O13'
或
>>> {int(i): build_string(df, i) for i in df.iloc[0] if i > 0}
{37: 'C24H31O8--> C23H27O9--> C22H23O10--> C21H19O11--> C20H15O12--> C19H11O13',
74: 'C24H31O8--> C22H23O10--> C20H15O12',
111: 'C24H31O8--> C21H19O11',
148: 'C24H31O8--> C20H15O12',
185: 'C24H31O8--> C19H11O13'}
答案 1 :(得分:0)
IIUC:
#!/usr/bin/env bash
#--------find apk---------
apkFile=$(find -name '*.apk' -print | grep -oP '(?<=.).*(.apk)')
#--------find apkFilePath---------
if test -z "apkFile"
then
echo "apkFile: is NULL"
exit 0;
fi
echo "apkFile: ${apkFile}"
apkFilePath=$(pwd)${apkFile}
echo "apk file path: ${apkFilePath}"
#--------install---------
if test -z "$apkFilePath"
then
echo "apkFilePath: is NULL"
exit 0;
fi
echo "adb install -t -r ${apkFilePath}"
for SERIAL in $(adb devices | grep -v List | cut -f 1);
do `adb -s ${SERIAL} install -t -r ${apkFilePath}`;
done
#--------get applicationId---------
echo "aapt dump badging ${apkFilePath} | grep -oP '(?<=package: name=).*(?=versionCode)'"
applicationId=$(aapt dump badging ${apkFilePath} | grep -oP '(?<=package: name=).*(?=versionCode)')
echo "applicationId: is ${applicationId}"
#--------launch---------
if test -z "$applicationId"
then
echo "applicationId: is NULL"
exit 0;
fi
echo "____________________START_APPLICATION_ID________________________"
echo "applicationId: ${applicationId}"
echo "____________________END_APPLICATION_ID__________________________"
echo "____________________START_LAUNCHER______________________________"
for SERIAL in $(adb devices | grep -v List | cut -f 1);
do `adb -s ${SERIAL} shell monkey -p ${applicationId} -c android.intent.category.LAUNCHER 1`;
done
echo "____________________END_LAUNCHER________________________________"
输出(df = pd.DataFrame(values, columns = columns)
s = df.where(df>0).stack()
out_dict = s.index.get_level_values(1).groupby(s)
):
out_dict