通过行分隔的df创建列表列表

时间:2019-12-20 17:46:26

标签: python pandas loops

我目前正在尝试为数据框中的每一行生成一个lsit列表。这是我一直在使用的代码:

lst = [x[i].tolist() for i in df.columns]

但是,此代码将为每一列创建一个列表。有什么办法可以按行分开吗?这样我就可以按列将每列的值列出来?

2 个答案:

答案 0 :(得分:0)

这是您要做什么吗?

初始化列表列表

int findDifferenceInDays(char* dateStr) 
{   
    // Date/time format example: 
    // 12.09.2018 13:44:30
    char date[] = "12.09.2018 13:44:30";
    struct tm *now, *parsedTime;
    time_t t_now = time(0);
    now = localtime(&t_now);
    time_t temp_time = time(&temp_time);
    parsedTime = localtime(&temp_time);
    int day, month, year, hour, minutes, seconds;
    sscanf(dateStr, "%d.%d.%d %d:%d:%d",
        &day, &month, &year, &hour, &minutes, &seconds);
    parsedTime->tm_year = year - 1990;
    parsedTime->tm_mon = month - 1;
    parsedTime->tm_mday = day;
    parsedTime->tm_hour = hour;
    parsedTime->tm_min = minutes;
    parsedTime->tm_sec = seconds;
    time_t endTime, begTime; 
    endTime = mktime(now);
    begTime = mktime(parsedTime);
    double diffInSeconds = difftime(endTime, begTime);
    int daysDifference = difftime(endTime, begTime) / (60*60*24);
    printf("Days between %s and now: %lf\n", dateStr, daysDifference / (60 * 60 * 24));
    return daysDifference;
}

创建熊猫数据框

data = [['dogs', 10], ['cows', 15], ['bulls', 20]]  

打印数据框。

df = pd.DataFrame(data, columns = ['Animals', 'Total'])  

答案 1 :(得分:0)

尝试创建列表列表的简单循环:

function start_wp_session(){
    if( !session_id() )
        session_start();
}
add_action('init','start_wp_session');