I have a made a dictionary and tried to call out the value but I am getting value of the excel cell row number also along with the cell value. I only want the values of the respective cells. Here's what I am getting while I run the code. I have attached the images. And why does it show dtype: object. Am I missing something in the code? or something is wrongly written.
def Trainreadin(actfile, actsheetname):
frame = Input.xclReadIn(actfile, actsheetname)
dict = {'resistance':
[']resistance_name(word)_A(N)_B(N/(km/h))_C(N/(km/h)2)_windT(km/h)_windB(km/h)', 'table']
outputdict = {key: framehandle.value_readin(value) for (key, value) in dict.items()}
outputdict["resistance_A"] = outputdict["resistance"][1]
outputdict["resistance_B"] = outputdict["resistance"][2] * 3.6
print(outputdict["resistance_A"])
return outputdict
Related
I'm importing a CSV to a dictionary, where there are a number of houses labelled (I.E. 1A, 1B,...)
Rows are labelled containing some item such as 'coffee' and etc. In the table is data indicating how much of each item each house hold needs.
Excel screenshot
What I am trying to do it check the values of the key value pairs in the dictionary for anything that isn't blank (containing either 1 or 2), and then take the key value pair and the 'PRODUCT NUMBER' (from the csv) and append those into a new list.
I want to create a shopping list that will contain what item I need, with what quantity, to which household.
the column containing 'week' is not important for this
I import the CSV into python as a dictionary like this:
import csv
import pprint
from typing import List, Dict
input_file_1 = csv.DictReader(open("DATA CWK SHOPPING DATA WEEK 1 FILE B.xlsb.csv"))
table: List[Dict[str, int]] = [] #list
for row in input_file_1:
string_row: Dict[str, int] = {} #dictionary
for column in row:
string_row[column] = row[column]
table.append(string_row)
I found on 'geeksforgeeks' how to access the pair by its value. however when I try this in my dictionary, it only seems to be able to search for the last row.
# creating a new dictionary
my_dict ={"java":100, "python":112, "c":11}
# list out keys and values separately
key_list = list(my_dict.keys())
val_list = list(my_dict.values())
# print key with val 100
position = val_list.index(100)
print(key_list[position])
I also tried to do a for in range loop, but that didn't seem to work either:
for row in table:
if row["PRODUCT NUMBER"] == '1' and row["Week"] == '1':
for i in range(8):
if string_row.values() != ' ':
print(row[i])
Please, if I am unclear anywhere, please let me know and I will clear it up!!
Here is a loop I made that should do what you want.
values = list(table.values())
keys = list(table.keys())
new_table = {}
index = -1
for i in range(values.count("")):
index = values.index("", index +1)
new_table[keys[index]] = values[index]
If you want to remove those values from the original dict you can just add in
d.pop(keys[index]) into the loop
I have defined 10 different DataFrames A06_df, A07_df , etc, which picks up six different data point inputs in a daily time series for a number of years. To be able to work with them I need to do some formatting operations such as
A07_df=A07_df.fillna(0)
A07_df[A07_df < 0] = 0
A07_df.columns = col # col is defined
A07_df['oil']=A07_df['oil']*24
A07_df['water']=A07_df['water']*24
A07_df['gas']=A07_df['gas']*24
A07_df['water_inj']=0
A07_df['gas_inj']=0
A07_df=A07_df[['oil', 'water', 'gas','gaslift', 'water_inj', 'gas_inj', 'bhp', 'whp']]
etc for a few more formatting operations
Is there a nice way to have a for loop or something so I don’t have to write each operation for each dataframe A06_df, A07_df, A08.... etc?
As an example, I have tried
list=[A06_df, A07_df, A08_df, A10_df, A11_df, A12_df, A13_df, A15_df, A18_df, A19_df]
for i in list:
i=i.fillna(0)
But this does not do the trick.
Any help is appreciated
As i.fillna() returns a new object (an updated copy of your original dataframe), i=i.fillna(0) will update the content of ibut not of the list content A06_df, A07_df,....
I suggest you copy the updated content in a new list like this:
list_raw = [A06_df, A07_df, A08_df, A10_df, A11_df, A12_df, A13_df, A15_df, A18_df, A19_df]
list_updated = []
for i in list_raw:
i=i.fillna(0)
# More code here
list_updated.append(i)
To simplify your future processes I would recommend to use a dictionary of dataframes instead of a list of named variables.
dfs = {}
dfs['A0'] = ...
dfs['A1'] = ...
dfs_updated = {}
for k,i in dfs.items():
i=i.fillna(0)
# More code here
dfs_updated[k] = i
I have a dataframe which looks like the following (Name of the first dataframe(image below) is relevantdata in the code):
I want the dataframe to be transformed to the following format:
Essentially, I want to get the relevant confirmed number for each Key for all the dates that are available in the dataframe. If a particular date is not available for a Key, we make that value to be zero.
Currently my code is as follows (A try/except block is used as some Keys don't have the the whole range of dates, hence a Keyerror occurs the first time you refer to that date using countrydata.at[date,'Confirmed'] for the respective Key, hence the except block will make an entry of zero into the dictionary for that date):
relevantdata = pandas.read_csv('https://raw.githubusercontent.com/open-covid-19/data/master/output/data_minimal.csv')
dates = relevantdata['Date'].unique().tolist()
covidcountries = relevantdata['Key'].unique().tolist()
data = dict()
data['Country'] = covidcountries
confirmeddata = relevantdata[['Date','Key','Confirmed']]
for country in covidcountries:
for date in dates:
countrydata = confirmeddata.loc[lambda confirmeddata: confirmeddata['Key'] == country].set_index('Date')
try:
if (date in data.keys()) == False:
data[date] = list()
data[date].append(countrydata.at[date,'Confirmed'])
else:
data[date].append(countrydata.at[date,'Confirmed'])
except:
if (date in data.keys()) == False:
data[date].append(0)
else:
data[date].append(0)
finaldf = pandas.DataFrame(data = data)
While the above code accomplished what I want in getting the dataframe in the format I require, it is way too slow, having to loop through every key and date. I want to know if there is a better and faster method to doing the same without having to use a nested for loop. Thank you for all your help.
I'm trying to create a prettytable with 4 columns. The information I'm trying to insert per row are located in different python dictionary. I would assume the code below will work but i'm receiving the error below
Exception: Row has incorrect number of values, (actual) 2!=4 (expected)
My code is:
t = PrettyTable(['key', 'Start', 'End', 'Retention'])
for key, val in total.items():
t.add_row([key, val])
for key, val in dic.items():
t.add_row([key, val])
print(t)
If you want to use values from both the total and dic dictionaries, You can use zip to iterate over them pairwise:
table = PrettyTable(['key', 'Start', 'End', 'Retention'])
for col_1_2, col_3_4 in zip(total.items(), dic.items()):
table.add_row([*col_1_2, *col_3_4])
your table is expecting 4 values for each row whereas you are entering only two values, that's the reason you are getting the error, so try this
for (k,v), (k2,v2) in zip(total.items(), dic.items()):
t.add_row([k, v, k2, v2])
I am trying to implement a customized automata where the transition table looks like:
The table is dynamic i.e. the column heading, row name, and data at every cell can be determined at run time. The column name and row name are also necessary.
I tried this code
table = []
table.append(["A",[0,["B",2],["C1",2]],[1,["C1",1]]])
table.append(["B",[0,["C",1]],[1,["C2",1]]])
table.append(["C",[0,["C1",1]],[1,["C2",1]]])
but I am unable to access the individual item in the cell i.e. B or 2 from B:2 etc. Then I tried
row = ["A","B","C","C1","C2"]
col = [0,1]
table = []
table.append([[["B",2],["C1",2]],["C1",1]])
table.append([["C",1],["C2",1]])
table.append([["C1",1],["C2",1]])
print(table[0][0][0][0])
Now, I can access the individual item (B in the above case) but I am lost with the four subscript. Specially, when I do not know the depth of the list in advance. Need to get some help to do it in some easy way. Being a novice, I will appreciate some explanation to the pythonic code.
Update: This is Non-deterministic Finite Automata. I tried the automaton package but they are not solving my problem. Following the solution of Tadhg-Mcdonald-Jensen, it give the correct out put for the first row (A) in the table but an error message for second row (B). Here is the code
table = {}
table["A"] = {0: {"B":2, "C1":2}, 1: {"C1":1}}
table["B"] = {0: {"C":1}, 1: {"C2",1}}
table["C"] = {0: {"C1":1}, 1: {"C2",1}}
for key,value in table["A"][0].items(): \\ok treated as dictionary (1)
print(key, value, sep="\t")
for key,value in table["A"][1].items(): \\ok treated as dictionary (2)
print(key, value, sep="\t")
for key,value in table["B"][0].items(): \\ok treated as dictionary (3)
print(key, value, sep="\t")
for key,value in table["B"][1].items(): \\wrong: why treated as set? Although same as (2)
print(key, value, sep="\t") \\Error message: AttributeError: 'set' object has no attribute 'items'
The output is
B 2
C1 2
C1 1
C 1
Traceback (most recent call last):
File "C:/Users/Abrar/Google Drive/Tourism Project/Python Projects/nestedLists.py", line 17, in <module>
for key,value in table["B"][1].items():
AttributeError: 'set' object has no attribute 'items'
Pandas is great at doing tables but you could also move to dictionaries, either way, list is not the data structure you want.
table = {}
table["A"] = {0: {"B":2, "C1":2}, 1: {"C1":1}}
table["B"] = {0: {"C":1}, 1: {"C2":1}}
table["C"] = {0: {"C1":1}, 1: {"C2":1}}
Then table["A"][0] will give you the first element, each element will have one or more entries, if you wanted to iterate over the entries you can do for key,value in table["A"][0].items()
Or to iterate over the entire table you could use 3 nested for loops:
#do_stuff = print
for row, line in table.items():
#each row in the table, row will go through ("A", "B", "C")
for column, cell in line.items():
#each cell in the row, column will go through (0, 1)
for label, value in cell.items():
#each entry in cell, most only have one entry except table["A"][0]
do_stuff(row, column, label, value)
To be honest I don't understand what the table represents so I can't give you specific advice but I think this would at least be a clearer data structure.