gpt4 book ai didi

python - Pandas 数据框 - 使用一条记录创建数据框

转载 作者:行者123 更新时间:2023-11-30 22:48:29 25 4
gpt4 key购买 nike

我想创建一个数据框而不从 CSV 读取它。

例如,我想创建列和一条记录。请假设这样的事情:

    Feature1 Feature 2  Feature 3 ... Feature n
1 20 False 3.2 True

我构建了一个分类器,我想做出预测: 分类器.预测(数据框)

我收到的记录是在特征之间带有“,”的字符串。我使用 split 来提取特征列表:

record_features = "16,713,Danny, ..."
features = record_features.split(',')

之后我将列表转换为系列:

series = pd.Series(features)

之后我想创建一个数据框: column_names = ['feature1', 'feature2', ..., 'feature102']

 df = pd.DataFrame(series, columns=column_names)

我收到一个错误:

ValueError: Shape of passed values is (1, 102), indices imply (102, 102)

我确实有 102 个功能,我想创建一个包含列的数据框和一条记录

有什么建议吗?

最佳答案

您可以添加[]:

column_names = ['Feature1','Feature2','Feature102']
record_features = "16,713,Danny"
features = record_features.split(',')

df = pd.DataFrame([features], columns=column_names)
print (df)
Feature1 Feature2 Feature102
0 16 713 Danny

另一个 numpy 解决方案 reshape :

df = pd.DataFrame(np.array(features)
.reshape(len(features) // len(column_names), len(column_names)),
columns=column_names)
print (df)
Feature1 Feature2 Feature102
0 16 713 Danny

时间:

column_names = ['Feature' + str(x) for x in range(102)]
record_features = "16,713,Danny"
features = record_features.split(',')
features = features * 34

In [222]: %timeit pd.DataFrame([features], columns=column_names)
100 loops, best of 3: 5.94 ms per loop

In [223]: %timeit pd.DataFrame(dict(zip(column_names, features)), index=[0], columns=column_names)
The slowest run took 4.48 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 5.25 ms per loop

In [224]: %timeit pd.DataFrame(np.array(features).reshape(len(features) // len(column_names), len(column_names)), columns=column_names)
The slowest run took 5.60 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 206 µs per loop

关于python - Pandas 数据框 - 使用一条记录创建数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40149650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com