使用列表作为行中的值创建Pandas数据框 [英] Create Pandas dataframe with list as values in rows

查看:163
本文介绍了使用列表作为行中的值创建Pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以以下格式创建熊猫数据框:

How to create pandas dataframe in the following format:

      A            B            C             D
0    [1,2,3,4]    [2,3,4,5]     [4,5,5,6]     [6,3,4,5]
1    [2,3,5,6]    [3,4,6,6]     [3,4,5,7]     [2,6,3,4]
2    [8,9,6,7]    [5,7,9,5]     [3,7,9,5]     [5,7,9,8]

基本上每一行都有一个列表作为元素.我正在尝试使用机器学习对数据进行分类.每个数据点具有40 x 6的值.是否有其他适合输入分类器的格式.

Basically each row has a list as elements. I am trying to classify data using machine learning. Each data point has 40 x 6 values. Is there any other format which is suitable to be fed into classifier.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plot

from sklearn.neighbors import KNeighborsClassifier

# Read csv data into pandas data frame
data_frame = pd.read_csv('data.csv')

extract_columns = ['LinearAccX', 'LinearAccY', 'LinearAccZ', 'Roll', 'pitch', 'compass']

# Number of sample in one shot
samples_per_shot = 40

# Calculate number of shots in dataframe
count_of_shots = len(data_frame.index)/samples_per_shot

# Initialize Empty data frame
training_index = range(count_of_shots)
training_data_list = []

# flag for backward compatibility
make_old_data_compatible_with_new = 0

if make_old_data_compatible_with_new:
    # Convert 40 shot data to 25 shot data
    # New logic takes 25 samples/shot
    # old logic takes 40 samples/shot
    start_shot_sample_index = 9
    end_shot_sample_index = 34
else:
    # Start index from 1 and continue till lets say 40
    start_shot_sample_index = 1
    end_shot_sample_index = samples_per_shot

# Extract each shot into pandas series
for shot in range(count_of_shots):
    # Extract current shot
    current_shot_data = data_frame[data_frame['shot_no']==(shot+1)]

    # Select only the following column
    selected_columns_from_shot = current_shot_data[extract_columns]

    # Select columns from selected rows
    # Find start and end row indexes
    current_shot_data_start_index = shot * samples_per_shot + start_shot_sample_index
    current_shot_data_end_index = shot * samples_per_shot + end_shot_sample_index
    selected_rows_from_shot = selected_columns_from_shot.ix[current_shot_data_start_index:current_shot_data_end_index]

# Append to list of lists
# Convert selected short into multi-dimensional array
training_data_list.append([selected_columns_from_shot[extract_columns[index]].values.tolist() for index in range(len(extract_c    olumns))])

# Append each sliced shot into training data
training_data = pd.DataFrame(training_data_list, columns=extract_columns)
training_features = [1 for i in range(count_of_shots)]
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(training_data, training_features)

推荐答案

简单

pd.DataFrame(
    [[[1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 5, 6], [6, 3, 4, 5]],
     [[2, 3, 5, 6], [3, 4, 6, 6], [3, 4, 5, 7], [2, 6, 3, 4]],
     [[8, 9, 6, 7], [5, 7, 9, 5], [3, 7, 9, 5], [5, 7, 9, 8]]],
    columns=list('ABCD')
)

使用MultiIndexunstack

lst = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [4, 5, 5, 6],
    [6, 3, 4, 5],
    [2, 3, 5, 6],
    [3, 4, 6, 6],
    [3, 4, 5, 7],
    [2, 6, 3, 4],
    [8, 9, 6, 7],
    [5, 7, 9, 5],
    [3, 7, 9, 5],
    [5, 7, 9, 8]]

pd.Series(lst, pd.MultiIndex.from_product([[0, 1, 2], list('ABCD')])).unstack()

              A             B             C             D
0  [1, 2, 3, 4]  [2, 3, 4, 5]  [4, 5, 5, 6]  [6, 3, 4, 5]
1  [2, 3, 5, 6]  [3, 4, 6, 6]  [3, 4, 5, 7]  [2, 6, 3, 4]
2  [8, 9, 6, 7]  [5, 7, 9, 5]  [3, 7, 9, 5]  [5, 7, 9, 8]

这篇关于使用列表作为行中的值创建Pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆