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

技术需求:获取用户输入并存储至DataFrame后打印表格

Complete Solution for User Input to pandas DataFrame

Let's wrap up this task with a polished, functional solution that builds on your existing input code. Here's how to collect the required details, store them in a pandas DataFrame, and print a clean table:

import pandas as pd

# --- Integrate your existing input code here, or use this full version ---
# Gather user input for each field
make = input("Enter the vehicle make: ")
model = input("Enter the vehicle model: ")
vehicle_type = input("Enter the vehicle type (e.g., sedan, SUV): ")
# Add error handling for numeric rating input
try:
    rating = float(input("Enter the vehicle rating (0-5): "))
except ValueError:
    print("Oops, that's not a valid number! Using default rating 3.0")
    rating = 3.0

# Structure input data into a dictionary (matches DataFrame column format)
vehicle_data = {
    "Make": [make],
    "Model": [model],
    "Type": [vehicle_type],
    "Rating": [rating]
}

# Convert the dictionary to a pandas DataFrame
vehicle_df = pd.DataFrame(vehicle_data)

# Print the formatted table (hide default index for cleaner output)
print("\nYour Vehicle Data Summary:")
print(vehicle_df.to_string(index=False))

Quick Breakdown of Key Parts:

  • Input Robustness: The try-except block prevents the script from crashing if the user enters a non-numeric rating, which adds a nice layer of error handling.
  • DataFrame Setup: Using a dictionary with column names as keys and lists as values ensures pandas creates the correct column structure, even for a single entry.
  • Clean Printing: to_string(index=False) removes the default row numbering, making the output look like a proper table instead of raw DataFrame output.

If you already have some input logic written, just swap out the input section with your existing code—this solution integrates seamlessly with what you've started.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:32:28