【问题标题】:Extract a column data with Input command in a csv file in python在python的csv文件中使用输入命令提取列数据
【发布时间】:2021-09-15 11:13:37
【问题描述】:

我需要通过输入命令提取具有用户定义的列名的列数据。用户应输入列名,并相应地显示或处理特定的列数据以供进一步计算。

这是我的代码。

# Importing required libraries
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sb
  
# Read the data into a series
df=pd.read_csv("Modified.csv")

#Input Column Name
col1=str(input("Enter Column Name (Case sensitive) :  "))
col2=str(input("Enter Column Name (Case sensitive) :  "))

# Creating a series of data 
x=df[['col1']]
y=df[['col2']]

#Calculate mean and Standard deviation.

mean_x = np.mean(x)
sd_x = np.std(x)

mean_y = np.mean(y)
sd_y = np.std(y)

【问题讨论】:

  • 这里没有问题,没有错误?用 col1 替换 'col1' 和用 col2 替换 'col2' 没有 '
  • 我已经做到了,现在面临这个错误。 ValueError:如果使用所有标量值,则必须传递索引

标签: python pandas dataframe python-2.7 numpy


【解决方案1】:
import pandas as pd
df = pd.read_csv("example.csv")
x1 = input("write the column name?")

如果 x1 in df.columns 此代码将正常工作:

value_mean = df[x1].mean()
value_std = df[x1].std()

您可以将上述代码用于任意数量的输入。 你也可以使用循环来获得高输入和操作:

for i in range(2):
    x = input("column name?")
    mean = df[x].mean()
    std = df[x].std()

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 2021-05-22
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多