User input shows value in other data frame column
Question:
I am trying to get the value of a corresponding cell in an adjacent column to show it’s value after a user puts in a variable.
Example of the data frame:
Price
Cap
0
2.5
8.5032
1
3.0
7.1208
2
3.5
5.9551
3
4.0
4.9444
4
4.5
5.2753
5
5.0
4.2754
Prompt the user for price and have it return the corresponding value in the Cap column.
The problem I am getting are the results are returning Empty DataFrame when I’m expecting a float value from the Cap column.
Example:
Input Price: 3.0
Cap: 7.1208
The data is being pulled from a csv and not created within the code; if that matters.
Cap column has been converted to float data type from string value.
I’ve tried to leave the Cap column data as a string and the results still fail.
Created Data Frame is labeled pdfrm
Example Code:
import numpy as np
import pandas as pd
prices = "/Users/Me/Desktop/file.csv"
pdfrm = pd.read_csv(prices)
pdfrm.drop(['Column3','Column4','Column5'],axis = 1, inplace = True)
pdfrm.fillna(0, inplace = True)
pdfrm.columns.str.replace(' ','')
pdfrm.rename(columns={'Column1':'Price', 'Column2':'Cap'}, inplace=True)
pdfrm['Cap'] = pdfrm['Cap'].str.replace('%','')
pdfrm['Cap'] = pdfrm['Cap'].astype('float') / 100
p = input('What is the price:')
c = (pdfrm.loc[pdfrm['Cap'] == p])
print('Cap:',c)
or
p = input('What is the price:')
c = (pdfrm.loc[pdfrm['Price'] == p])
print('Cap:',c)
Results:
What is the price:3.0
Cap: Empty DataFrame
Columns: [Price, Cap]
Index: []
What I am looking for is:
What is the price:3.0
Cap:7.1208
I’ve removed all NaN values and replaced them with 0.00. All white space has been removed from within the data frame.
Overall the data frame is a two column, 39 row data set that will constantly change and I’m not sure if the code I’m running that specifically needs user input would accomplish what I am looking for.
I’m open to all suggestions.
Answers:
The main problem was that input()
returns str
type but you compare with float
like "0.04"==0.04 and it’s not equal.
Here’s working code with pandas table you provided:
pandas_file.py
import numpy as np
import pandas as pd
prices = "data.csv"
pdfrm = pd.read_csv(prices)
pdfrm['Price'] = pdfrm['Price'].astype('float') / 100
# Enter 0.04
p = float(input('What is the price:')) # input() return str type so it should be converted to float
#print(type(p)) # To check variable type
c = (pdfrm.loc[pdfrm['Price'] == p])
print('Cap:',c)
data.csv
Price,Cap
2.5,8.5032
3.0,7.1208
3.5,5.9551
4.0,4.9444
4.5,5.2753
5.0,4.2754
Also in your code c = (pdfrm.loc[pdfrm['Cap'] == p])
it should check for 'Price'
If it doesn’t work it might be problem with data processing step (where you remove spaces and extra characters) to check if it’s good do print(pdfrm)
output should look well formmated like this:
Price Cap
0 2.5 8.5032
1 3.0 7.1208
2 3.5 5.9551
3 4.0 4.9444
4 4.5 5.2753
5 5.0 4.2754
Hope it helps
I am trying to get the value of a corresponding cell in an adjacent column to show it’s value after a user puts in a variable.
Example of the data frame:
Price | Cap | |
---|---|---|
0 | 2.5 | 8.5032 |
1 | 3.0 | 7.1208 |
2 | 3.5 | 5.9551 |
3 | 4.0 | 4.9444 |
4 | 4.5 | 5.2753 |
5 | 5.0 | 4.2754 |
Prompt the user for price and have it return the corresponding value in the Cap column.
The problem I am getting are the results are returning Empty DataFrame when I’m expecting a float value from the Cap column.
Example:
Input Price: 3.0
Cap: 7.1208
The data is being pulled from a csv and not created within the code; if that matters.
Cap column has been converted to float data type from string value.
I’ve tried to leave the Cap column data as a string and the results still fail.
Created Data Frame is labeled pdfrm
Example Code:
import numpy as np
import pandas as pd
prices = "/Users/Me/Desktop/file.csv"
pdfrm = pd.read_csv(prices)
pdfrm.drop(['Column3','Column4','Column5'],axis = 1, inplace = True)
pdfrm.fillna(0, inplace = True)
pdfrm.columns.str.replace(' ','')
pdfrm.rename(columns={'Column1':'Price', 'Column2':'Cap'}, inplace=True)
pdfrm['Cap'] = pdfrm['Cap'].str.replace('%','')
pdfrm['Cap'] = pdfrm['Cap'].astype('float') / 100
p = input('What is the price:')
c = (pdfrm.loc[pdfrm['Cap'] == p])
print('Cap:',c)
or
p = input('What is the price:')
c = (pdfrm.loc[pdfrm['Price'] == p])
print('Cap:',c)
Results:
What is the price:3.0
Cap: Empty DataFrame
Columns: [Price, Cap]
Index: []
What I am looking for is:
What is the price:3.0
Cap:7.1208
I’ve removed all NaN values and replaced them with 0.00. All white space has been removed from within the data frame.
Overall the data frame is a two column, 39 row data set that will constantly change and I’m not sure if the code I’m running that specifically needs user input would accomplish what I am looking for.
I’m open to all suggestions.
The main problem was that input()
returns str
type but you compare with float
like "0.04"==0.04 and it’s not equal.
Here’s working code with pandas table you provided:
pandas_file.py
import numpy as np
import pandas as pd
prices = "data.csv"
pdfrm = pd.read_csv(prices)
pdfrm['Price'] = pdfrm['Price'].astype('float') / 100
# Enter 0.04
p = float(input('What is the price:')) # input() return str type so it should be converted to float
#print(type(p)) # To check variable type
c = (pdfrm.loc[pdfrm['Price'] == p])
print('Cap:',c)
data.csv
Price,Cap
2.5,8.5032
3.0,7.1208
3.5,5.9551
4.0,4.9444
4.5,5.2753
5.0,4.2754
Also in your code c = (pdfrm.loc[pdfrm['Cap'] == p])
it should check for 'Price'
If it doesn’t work it might be problem with data processing step (where you remove spaces and extra characters) to check if it’s good do print(pdfrm)
output should look well formmated like this:
Price Cap
0 2.5 8.5032
1 3.0 7.1208
2 3.5 5.9551
3 4.0 4.9444
4 4.5 5.2753
5 5.0 4.2754
Hope it helps