Answers for "iterate over column names in dataframe r"

4

python loop through column in dataframe

# Iterate over two given columns only from the dataframe
for column in empDfObj[['Name', 'City']]:
   # Select column contents by column name using [] operator
   columnSeriesObj = empDfObj[column]
   print('Colunm Name : ', column)
   print('Column Contents : ', columnSeriesObj.values)
Posted by: Guest on March-04-2020
0

iterate through names of columns in result set pdo

//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);

//Create our SQL query.
$sql = "SELECT * FROM my_table_name LIMIT 20";

//Prepare our SQL query.
$statement = $pdo->prepare($sql);

//Executre our SQL query.
$statement->execute();

//Fetch all of the rows from our MySQL table.
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

//Get the column names.
$columnNames = array();
if(!empty($rows)){
    //We only need to loop through the first row of our result
    //in order to collate the column names.
    $firstRow = $rows[0];
    foreach($firstRow as $colName => $val){
        $columnNames[] = $colName;
    }
}
Posted by: Guest on May-06-2021

Code answers related to "iterate over column names in dataframe r"

Python Answers by Framework

Browse Popular Code Answers by Language