How to create a nested dictionary in python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hello all just learning dictionary in python. I have few data please let me know how to create a nested dictionary. Data are available below with duplicate values in excel file. Please do explain using for loop
Name Account Dept
John AC Lab1
Dev AC Lab1
Dilip AC Lab1,Lab2
Sat AC Lab1,Lab2
Dina AC Lab3
Surez AC Lab4
I need the result in below format:
{
'AC': {
'Lab1': ['John', 'Dev', 'Dilip', 'Sat'],
'Lab2': ['Dilip','Sat'],
'Lab3': ['Dina'],
'Lab4': ['Surez']
}
}

Something like this should get you closer to an answer but I'd need your input file to optimize it:
import xlrd
from collections import defaultdict
wb = xlrd.open_workbook("<your filename>")
sheet_names = wb.sheet_names()
sheet = wb.sheet_by_name(sheet_names[0])
d = defaultdict(defaultdict(list))
for row_idx in range(0, sheet.nrows):
cell_obj_0 = sheet.cell(row_idx, 0)
cell_obj_1 = sheet.cell(row_idx, 1)
cell_obj_2 = sheet.cell(row_idx, 2)
for lab in cell_obj_2.split(","):
d[cell_obj_1][lab].append(cell_obj_0)

Related

How to delete punctuation and number from element in the list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have a list with many nams of columns that i work with for my project.
my list is like this:
list_colunm = ['solar [W].1', 'Wind [W].02', 'Caz [W].33']
(and other elements it's a long list).
if you can help me with same methods to delete .1 .02 and .33
Standard Python:
list_column = ['solar [W].1', 'Wind [W].02', 'Caz [W].33']
list_shortnames = [x.rsplit('.')[0] for x in list_column]
Output:
['solar [W]', 'Wind [W]', 'Caz [W]']
Pandas:
The most simple way is using rename() with a dict as a map.
import pandas as pd
mapping = {"solar [W].1": "solar [W]", "Wind [W].02": "Wind [W]", "Caz [W].33": "Caz [W]"}
df.rename(mapping, inplace=True, axis='columns')
More flexible alternative (#mozway):
df.rename(columns=lambda x: x.rsplit('.', n=1)[0])
Output:
solar [W] Wind [W] Caz [W]
0 1 2 3

Get stock data problems [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was making getting stock data file and output was just
In Progress
[]
what's the matter?
import quandl
from datetime import datetime as dt
def get_stock_data(stock_ticker):
print("In Progress")
start_date = dt(2019, 1, 1)
end_date = dt.now()
quandl_api_key = "tJDGptkdfqwjYi123RVV"
quandl.ApiConfig.api_key = quandl_api_key
source = "WIKI/" + stock_ticker
data = quandl.get(source, start_date=str(start_date), end_date=str(end_date))
data = data[["Open", "High", "Low", "Volume", "Close"]].values
print(data)
return data
get_stock_data("AAPL")
There's nothing wrong with your code. However recent stock data is a Premium product from Quandl and I presume you are just on the free subscription, hence your dataframe comes back empty. If you change the dates to 2017, you will get some results but that's as far as it goes on the free subscription it seems.

Querying JSON with Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I have parsed the JSON with json.load.
Now I want to query that JSON dict using SQL-like commands. Does anything exist like this in Python? I tried using Pynq https://github.com/heynemann/pynq but that didn't work too well and I've also looked into Pandas but not sure if that's what I need.
Here is a simple pandas example with Python 2.7 to get you started...
import json
import pandas as pd
jsonData = '[ {"name": "Frank", "age": 39}, {"name": "Mike", "age":
18}, {"name": "Wendy", "age": 45} ]'
# using json.loads because I'm working with a string for example
d = json.loads(jsonData)
# convert to pandas dataframe
dframe = pd.DataFrame(d)
# Some example queries
# calculate mean age
mean_age = dframe['age'].mean()
# output - mean_age
# 34.0
# select under 40 participants
young = dframe.loc[dframe['age']<40]
# output - young
# age name
#0 39 Frank
#1 18 Mike
# select Wendy from data
wendy = dframe.loc[dframe['name']=='Wendy']
# output - wendy
# age name
# 2 45 Wendy

Average data by each hour in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have data like below:
Data Columns:
DateTime,Data1,Data2,Data3,Month,Date,Year,Hour,Minutes
1/1/2017 0:00,1.1,2.2,3.3,1,1,2017,0,00
1/1/2017 0:00,1.1,2.2,3.3,1,1,2017,0,15
1/1/2017 0:00,1.1,2.2,3.3,1,1,2017,0,30
1/1/2017 0:00,1.1,2.2,3.3,1,1,2017,1,45
I need to average columns 'WS', 'VWS' .... 'SR' data by each hour. The DateTime column is reported every 15 minutes.
I have an answer to my own question. Posting it here so that others can benefit:
import pandas as pd
df = pd.read_csv("MetData.csv")
df['NewDateTime'] = pd.to_datetime(df['DateTime'])
df.index = df['NewDateTime']
df_p = df.resample('H').mean()
df_p['Month'] = df['NewDateTime'].dt.month
df_p['Year'] = df['NewDateTime'].dt.year
df_p['Date'] = df['NewDateTime'].dt.day
df_p['Hour'] = df['NewDateTime'].dt.hour
writer = pd.ExcelWriter('MetData_Orig1.xlsx', engine='xlsxwriter')
df_p.to_excel(writer, sheet_name='Sheet1')
writer.save()

Storing data in Redis then fetch that data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have an Excel file that contains four columns. I want to fetch this data and store it in MySQL. Later on I want fetch the data from here and store in Redis, then run a validation on it. I have already done the importing of data from Excel to Python.
You have to reshape your 4 column excel data to a 1 column data.
The redis client for Matlab/GNU Octave is doing this e.g.: https://github.com/markuman/go-redis/wiki/Data-Structure#arrays
Take care that in this example, Matlab/Octave are using Column-Major-Order.
Python is using Row-Major-Order: https://en.wikipedia.org/wiki/Row-major_order
So you have to save your 4 column X rows data as a row-major-order list in redis as a list (RPUSH).
example
given this excel sheet
using this python3 code
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 20 23:02:53 2015
#author: markus
"""
import pandas as pd
import redis
# redis connection
r = redis.StrictRedis(host='localhost', port=6379, db=0)
# open the first worksheed
df = pd.read_excel('/home/markus/excel.xlsx',0)
# read in as a list
# [[1, 'two', 'python'], ['excel', 'redis', 'action']]
a = list(df.T.itertuples())
print("this is a, your excel list")
print(a)
for list in a:
for value in list:
r.rpush('myexceldata', str(value))
# read all back to python
b = r.lrange('myexceldata', '0', '-1')
print("A1 becomes 0, B1 becomes 3 ...")
print(b[3].decode('UTF-8'))
to save it serialized as a list in redis
127.0.0.1:6379> lrange myexceldata 0 -1
1) "1"
2) "two"
3) "python"
4) "excel"
5) "redis"
6) "action"
This is just one way to save a spreadsheet in redis. It always belong on your datastructure and what you're going to do with it.

Categories