【问题标题】:Asking user to input certain information要求用户输入某些信息
【发布时间】:2020-02-03 22:37:31
【问题描述】:

我有这段代码,它获取一个 csv 文件,按列过滤,然后绘制另一列的值。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
import numpy as np

df = pd.read_csv(r'C:/Desktop/Plot/dataframe.csv', delimiter=";", encoding='unicode_escape')
df['num_1'] = df['num_2'].str.split(',').str[0]
df['num_1'] = df['num_2'].astype('int64', copy=False)

X=df[df['Describe']=='The Start of Journey']['num_2'].values

dev_x= X 


# Set figure size
plt.figure(figsize=(10, 5))

plt.hist(dev_x, bins=5)


plt.title('Data')

这是数据集

+-----+-------+-----------------------+--------+--------+
|     | name  |       Describe        | num_1  | num_2  |
+-----+-------+-----------------------+--------+--------+
|  0  | er    | The Start of Journey  |    17  |  249,5 |
|  1  | NaN   | NaN                   |    58  |   51,0 |
|  2  | NaN   | NaN                   |    14  |   66,5 |
|  3  | NaN   | NaN                   |   526  |   84,0 |
|  4  | be    | The end of journey    |     3  |   13,0 |
|  5  | tg    | Levels                |   342  |   34,0 |
|  6  | NaN   | NaN                   |   231  |   55,6 |
|  7  | NaN   | NaN                   |    23  |   75,0 |
|  8  | tf    | counts                |    54  |   34,6 |
|  9  | sf    | The Start of Journey  |    52  | 4324,0 |
| 10  | gd    | The Start of Journey  |   352  |   54.0 |
+-----+-------+-----------------------+--------+--------+

我想修改代码,使其执行以下操作

  1. 提示用户添加 csv 文件
  2. 提示用户添加我们要过滤的列的名称(本例为Describe 列)
  3. 提示用户添加字符串(本例为The Start of Journey
  4. 提示用户添加我们要绘制数据的列的名称(本例为num_2

我已经检查了其他来源,但由于代码结构的原因,我在这方面遇到了麻烦。

【问题讨论】:

  • 如果您在浏览器中搜索“Python 用户输入”,您会找到比我们在此处管理的更能解释这一点的参考资料。
  • 同样投票关闭,读入输入实际上是一行代码,你可以从谷歌的第一个结果中找到,其余的都在使用 pandas API。去玩吧,如果你卡在某个特定的事情上,回来一定会有人帮忙的。
  • 您的问题是什么?您只是要求人们编写代码来满足您的要求吗?

标签: python pandas csv


【解决方案1】:

使用input() 函数。你可以有一个像x 这样的变量,然后做x = input("Enter the CSV path>>> ")(或类似的东西),x 将是一个带有任何用户输入的字符串。然后您可以稍后使用 x 。例如,您可以只输入x,而不是“描述”。

x = input("Enter the csv path>>>") # returns answer in string form

【讨论】:

    猜你喜欢
    • 2010-12-07
    • 2021-11-29
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2011-10-01
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多