You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从numpy array创建DataFrame?确认numpy数组维度是否正确

Answer

1. Dimension Description Check

Yes, your dimension description is completely correct!

In NumPy, the shape attribute of a 2D array returns a tuple formatted as (number_of_rows, number_of_columns). Since numpy_x.shape is (9982, 26), this directly translates to:

  • 9982 individual records/observations (each row in the array represents one unique observation)
  • 26 features/attributes per observation (each column corresponds to a distinct feature)

The sample array content you shared aligns perfectly with this structure—each sublist is a single observation containing 26 values, which matches the column count in your shape tuple.

2. Creating a Pandas DataFrame from the NumPy Array

Converting your NumPy array to a Pandas DataFrame is straightforward using the pandas.DataFrame() constructor. Here's how to do it:

Basic Conversion (Default Column Labels)

First, ensure Pandas is installed (run pip install pandas if you haven’t already), then use this code:

import numpy as np
import pandas as pd

# Convert the NumPy array to a DataFrame
df = pd.DataFrame(numpy_x)

This will generate a DataFrame with default column labels: 0, 1, 2, ..., 25.

Adding Custom Column Names

If you want meaningful names for your columns instead of numeric labels, pass a list of names to the columns parameter:

# Example: Generate a list of descriptive column names (adjust to your actual feature names)
column_names = [f"feature_{i+1}" for i in range(26)]
# Or use specific names like ["user_id", "temperature", "humidity", ...]

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

Verify the Result

To confirm the DataFrame was created correctly, you can print the first 5 rows:

print(df.head())

内容的提问来源于stack exchange,提问作者bgarcial

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:33:24