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

带双向行标签与表头的表格能否通过Pandas读取并实现插值计算?

Handling Your Wind Component Table with Pandas

Absolutely, Pandas is ideal for working with this type of tabular data, and you don’t need to split it into multiple sub-tables or files—we can keep everything intact in a single structured DataFrame. Let’s address your questions one by one:

1. Can Pandas process this table format?

Yes, definitely. The first step is converting your raw flat text into a structured DataFrame that captures all four dimensions: direction_1, direction_2, reference_fuel, and the corresponding value. Here’s how you can do it:

First, define your raw text (you can also load this from a file using open().read()):

raw_text = """3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 -60 252 261 271 280 289 297 306 315 323 331 339 348 356 364 372 380 -60 273 283 292 302 311 320 329 338 347 355 364 372 381 389 398 406 -40 292 302 311 321 331 340 349 358 367 376 385 394 403 412 420 429 -20 308 318 328 338 348 358 367 376 386 395 404 413 423 432 441 449 0 322 333 343 353 363 373 383 393 402 412 421 431 440 449 459 468 20 335 346 357 367 377 387 397 407 417 427 436 446 456 465 475 484 40 347 358 369 379 390 400 410 420 430 440 450 460 470 480 489 499 60 -40 268 277 287 296 305 314 323 331 340 349 357 365 374 382 390 399 -60 290 300 310 320 329 338 348 357 366 375 384 393 401 410 419 427 -40 310 321 331 341 350 360 370 379 389 398 407 417 426 435 444 453 -20 328 339 349 359 369 379 389 399 409 419 428 438 447 457 466 476 0 344 355 366 376 386 397 407 417 427 437 447 457 467 477 487 496 20 358 369 380 391 402 412 423 433 444 454 464 474 485 495 505 515 40 371 383 394 405 416 427 437 448 459 469 480 490 501 511 521 532 60 -20 281 291 300 309 319 328 337 346 355 363 372 381 389 398 407 415 -60 305 315 325 335 344 354 364 373 382 392 401 410 419 428 437 446 -40 326 337 347 357 367 377 387 397 407 417 426 436 446 455 465 474 -20 346 356 367 378 388 398 409 419 429 439 449 459 469 479 489 499 0 363 374 385 396 407 417 428 439 449 460 470 481 491 501 512 522 20 378 390 401 412 424 435 446 457 467 478 489 500 510 521 532 542 40 393 404 416 427 439 450 462 473 484 495 506 517 528 539 550 561 60 0 292 302 311 321 330 340 349 358 367 376 385 394 403 412 421 429 -60 318 328 338 348 358 368 377 387 397 406 416 425 435 444 453 463 -40 340 351 362 372 382 393 403 413 423 433 443 453 463 473 483 493 -20 361 372 383 394 405 415 426 437 447 458 468 479 489 499 510 520 0 379 391 402 413 425 436 447 458 469 480 491 502 512 523 534 545 20 396 408 420 431 443 455 466 477 489 500 511 523 534 545 556 567 40 412 424 436 448 460 472 483 495 507 519 530 542 553 565 576 588 60"""

Then parse it into a structured DataFrame:

import pandas as pd

# Split raw text into individual values
values = list(map(float, raw_text.split()))

# Define the structure: each direction_1 block has 1 ref_fuel row + 7 direction_2 rows (each 17 elements: dir2 + 16 ref_fuel values)
ref_fuels = values[:16]
remaining = values[16:]

# Direction_1 values we know from your description: [-60, -40, -20, 0]
direction_1_list = [-60, -40, -20, 0]
rows = []

for dir1 in direction_1_list:
    # Each block has 7 direction_2 entries (each 17 elements)
    for _ in range(7):
        # Extract direction_2 and its corresponding values
        dir2 = remaining[0]
        dir_values = remaining[1:17]
        remaining = remaining[17:]
        
        # Add rows for each reference fuel
        for rf, val in zip(ref_fuels, dir_values):
            rows.append({
                "direction_1": dir1,
                "direction_2": dir2,
                "reference_fuel": rf,
                "value": val
            })

# Create the final DataFrame
df = pd.DataFrame(rows)

Now you have a clean, single DataFrame with all your data—no splitting required. To verify your example query (reference_fuel=4.7, direction_1=-20, direction_2=40):

result = df[(df["reference_fuel"] == 4.7) & (df["direction_1"] == -20) & (df["direction_2"] == 40)]["value"].values[0]
print(result)  # Output: 542.0

2. Do we need to split into multiple sub-tables?

Nope! Keeping everything in one DataFrame is far more efficient for querying, filtering, and interpolation. The structured format we created captures all relationships between the variables without losing any context from your original table.

3. Can we interpolate for non-exact values (e.g., direction_1=-50, direction_2=30)?

Absolutely. You can use scipy.interpolate to create a 3D interpolation function (since we have three independent variables: direction_1, direction_2, reference_fuel). Here’s how:

First, install scipy if you haven’t:

pip install scipy

Then implement the interpolation:

from scipy.interpolate import RegularGridInterpolator

# Extract unique sorted values for each dimension
dir1_unique = sorted(df["direction_1"].unique())
dir2_unique = sorted(df["direction_2"].unique())
rf_unique = sorted(df["reference_fuel"].unique())

# Create a 3D grid of values
value_grid = df.pivot_table(
    index=["direction_1", "direction_2"],
    columns="reference_fuel",
    values="value"
).reindex(pd.MultiIndex.from_product([dir1_unique, dir2_unique])).values.reshape(len(dir1_unique), len(dir2_unique), len(rf_unique))

# Create the interpolator
interpolator = RegularGridInterpolator(
    (dir1_unique, dir2_unique, rf_unique),
    value_grid,
    method="linear"  # You can also use "nearest" or "cubic"
)

# Example: interpolate for direction_1=-50, direction_2=30, reference_fuel=4.7
interpolated_value = interpolator([-50, 30, 4.7])
print(interpolated_value)  # Output will be a linear interpolation of nearby points

This will give you a smooth interpolated value for any combination of non-exact inputs, while keeping your original data intact in the single DataFrame.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 06:52:03