我有2个数据框;
import pandas as pd
data1 = {"name": ["a","b","c","d","e"], "value": [0.01,0.02,0.03,0.04,0.05]}
df1 = pd.DataFrame (data1, columns = ['name', 'value'])
data2 = {"value": [0.01,0.03,0.05]}
df2 = pd.DataFrame (data2, columns = ['value'])
我想使用df2中的值来查找df1中对应的名称,并将其添加为新列。即以这个df结尾:
data3 = {"value": [0.01,0.03,0.05], "name": ["a","c","e"]}
df3 = pd.DataFrame (data3, columns = ['value', 'name'])
答案 0 :(得分:0)
您在这里:
$matrix = [
[
[3, 5, 13, 56],
[0, 1, 165, 1],
[-8, 78, 5, 8],
[6, 5, 23, 45]
],
[
[1, 17, 5, 3],
[1, 5, 1, 65],
[6, 5, 5, -4],
[0, 4, 3, 90]
],
[
[9, 9, 8, 0],
[3, 5, 4, 8],
[0, 5, 3, 9],
[1, 4, 5, 7]
]
];
function color(&$matrix){
for ($i = 0; $i < count($matrix); ++$i) {
echo 'layer ' . ($i + 1) . ':' . PHP_EOL;
foreach ($matrix as $j) {
if($i == 1){
$y=0;
foreach ($j as $k) {
if($y == 0 )
echo $f = 1 . ' ';
elseif($y == 1)
echo $f = 0 . ' ';
elseif($y == 2)
echo $f = 1 . ' ';
$y++;
}
}else{
foreach ($j as $k) {
echo $k = 1 . ' ';
}
}
echo PHP_EOL;
}
}
}
输出:
print(df2.merge(df1, on='value'))
答案 1 :(得分:0)
您可以在此处pd.Series.isin
中使用boolean indexing
并将其用作遮罩。
df1[df1['value'].isin(df2['value'])]
name value
0 a 0.01
2 c 0.03
4 e 0.05