根据熊猫中多个列中的值从DataFrame中选择行

时间:2019-08-09 00:15:12

标签: python pandas dataframe

因此this question涉及如何根据数组(或单个列)中的值选择数据帧中行的子集。我不足以解决我的问题。

我在多个目录中有许多不同的表。我有一本字典,其中包含表之间的关系(例如用于联接的键)。对于每个表T1,我查找共享相同列名(键)的其他表(T2,T3 ...),并且要过滤这些表(T2,T3 ...),以在表中包含具有匹配键值的行。 T1列集。按键设置可能有所不同! T1可以在一个列(键)上连接到T2,而T1可以在5个键上与T2连接!我事先不知道这一点。

例如,我有t1, t2, t3pks=["id"] (t1-->t2), fks=["id", "index", "zip"] (t1-->t3)

t1
id|index|zip|v
10|10000|200|20

t2
id|v
10|30
20|50
30|70

t3
id|index|zip|v
00|10000|200|10
10|10000|200|20
10|10000|300|30
10|10000|200|10

t2和t3的输出为

t2
id|v
10|30

和 t3

id|index|zip|v
10|10000|200|20
10|10000|200|10

看看前面的答案,我可能需要做类似的事情

filtered_t2 = t2.loc[t2[pks].isin(t1[fks])]

但是我收到以下错误消息

ValueError: Cannot index with multidimensional key

可能以这种方式我无法处理复合键,但是如果我只提供一个键-'id',也会失败。 所以也许它不能接受数组作为值...

pksfks是可变大小的数组时,如何处理?

这是正确的方法吗?

    filter = None
    for p, f in zip(pks, fks):
        if filter is None:
            filter = t2[p].isin(t1[f])
        else:
            filter &= t2[p].isin(t1[f])

    filtered_ft = t2.loc[filter]

谢谢!

1 个答案:

答案 0 :(得分:1)

让我们在这里尝试<!--https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart--> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart1); function drawChart1() { var dataTable = new google.visualization.DataTable(); dataTable.addColumn('string', 'Month'); //loop from above for store and store base <?php echo($cstores); ?> dataTable.addRows([ <?php $sql = "SELECT QUOTE(DATE_FORMAT(date_submitted,'%b-%y')) AS 'Month' ". $string ." FROM data WHERE store_list IN (" . $user_stores . ") GROUP BY YEAR(date_submitted), MONTH(date_submitted) ORDER BY YEAR(date_submitted) ASC, MONTH(date_submitted) ASC"; $query = mysqli_query($conn,$sql); foreach( $query as $key => $array ) { echo("["); foreach( $array as $attribute => $value ) { echo str_replace("|","\\n",$value) . ","; //couldn't put \\n in the SQL above as it rendered in the output and broke it, so replacing the | here with \\n so it renders properly in the chart } echo("],"); } ?> ]); var options = { title: '', vAxis: { minValue: 0, maxValue: 10, <?php if($submittedValue == 3){ // adjust chart scale and axis if MPO echo("format: '#\'%\'',"); echo("title: 'Percent',"); } else{ echo("title: 'Score 0-10'"); } ?> }, legend: { position: 'bottom', }, pointSize: 10, pointShape: 'diamond' }; var chart = new google.visualization.LineChart(document.getElementById('chart_div')); chart.draw(dataTable, options); }

merge

以其他方式

t2.merge(t1,how='inner',on=['id'])

t3.merge(t1,how='inner',on=['id','index','zip'])