Python - insert variable when defining a name [duplicate] - python

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a relatively stupid question which I am not able to formulate very well (and I think it explains why I am not finding any answer)
I would like to calculate the mean, minimum, and maximum of a panda series in my dataframe for many variable (let's say age and weight)
dataframe.age.min()
dataframe.age.max()
dataframe.age.mean()
dataframe.weight.min()
dataframe.weight.max()
dataframe.weight.mean()
I would like to create some kind of loop, which would do something like:
list = ['age','weight']
for x in list:
min-"x" = dataframe.x.min()
max-"x" = dataframe.x.max()
mean-"x" = dataframe.x.mean()
I would like to have variables called min-age, max-age, mean-age
I don't understand how to define a function, and how to insert in the name min-"x" the name of my variable (x)...

Use describe on you dataframe then manipulate the index.
dfd = df.describe().stack()
dfd.index = dfd.index.to_series().str.join('-')
count-age 10.000000
count-weight 10.000000
mean-age -0.200662
mean-weight 0.298352
std-age 1.175323
std-weight 0.901915
min-age -1.778043
min-weight -0.860798
25%-age -1.144173
25%-weight -0.488076
50%-age -0.092748
50%-weight 0.294160
75%-age 0.276348
75%-weight 0.892405
max-age 1.670823
max-weight 1.680473
dtype: float64

Related

Why does remove() method does not work in certain instances? [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 4 months ago.
mepstrip = ['AE38', 'AL29', 'AL30', 'AL35', 'AL41', 'BA37', 'BB37', 'BC37', 'CH24', 'CO26', 'GD29', 'GD30', 'GD35', 'GD38', 'GD41', 'GD46', 'PM29', 'SA24', 'T2V1', 'TDJ3']
indexlist = ['AE38', 'AE38C', 'AE38D', 'AL29', 'AL29C', 'AL29D', 'AL30', 'AL30C', 'AL30D', 'AL35', 'AL35C', 'AL35D', 'AL41', 'AL41C', 'AL41D', 'AY24C', 'BA37D', 'BA37E', 'BAY23', 'BB37D', 'BB37E', 'BC37D', 'BDC24', 'BDC28', 'CEDI', 'CH24D', 'CO23', 'CO26', 'CO26D', 'CUAP', 'DICP', 'DIP0', 'FORM3', 'GD29', 'GD29C', 'GD29D', 'GD30', 'GD30C', 'GD30D', 'GD35', 'GD35C', 'GD35D', 'GD38', 'GD38C', 'GD38D', 'GD41', 'GD41C', 'GD41D', 'GD46', 'GD46C', 'GD46D', 'PAP0', 'PARP', 'PBA25', 'PBY22', 'PM29D', 'PMM29', 'PMY24', 'PR13', 'PUL26', 'SA24D', 'SARH', 'SFA23', 'T2V1C', 'T2V1D', 'T2V2', 'T2V3', 'T2X3', 'T2X4', 'TB23P', 'TB24', 'TC23', 'TC25P', 'TDF24', 'TDJ23', 'TDJ3D', 'TDL23', 'TDS23', 'TFU27', 'TO23', 'TO26', 'TV23', 'TV24', 'TX22', 'TX23', 'TX24', 'TX25', 'TX26', 'TX28', 'TY22P', 'TY27P']
for i in mepstrip:
if i not in indexlist:
mepstrip.remove(i)
# ['BB37', 'CH24', 'SA24', 'TDJ3'] remains in mepstrip after for loop despite not in indexlist
You can't remove items from a list while iterating over it. Instead, you could make a copy of the list. And remove the items from that.
new_mepstrip = mepstrip.copy()
for i in mepstrip:
if i not in indexlist:
new_mepstrip.remove(i)

How do I use an input to call a function? [duplicate]

This question already has answers here:
How to access (get or set) object attribute given string corresponding to name of that attribute
(3 answers)
Closed 2 years ago.
how can I make a print to change to the input value?
import cryptowatch as cw
time = input("Time:") #15m, 1h , 1d
x = cw.markets.get("KRAKEN:ATOMEUR", ohlc = True, periods = [time])
print(x.of_15m[1][4]))
for example:
time = input("Time:") #1h
print(x.of_1h[1][4])
or:
time = input("Time:") #1d
print(x.of_1d[1][4])
EDIT:
I leave more information
cryptowatch-sdk
https://github.com/cryptowatch/cw-sdk-python
Module file where the functions are:(line 255)
https://github.com/cryptowatch/cw-sdk-python/blob/master/cryptowatch/resources/markets.py
I couldn't really test this properly since I don't have cryptowatch installed, but I think it would work. It uses the user's input to determine the name of an x object attribute, and then uses getattr() to retrieve its current value.
import cryptowatch as cw
time = input("Time:")
x = cw.markets.get("KRAKEN:ATOMEUR", ohlc=True, periods=[time])
interval = getattr(x, 'of_'+time, None)
if interval is not None:
print(interval[1][4])
else:
print('Error: unknown time', time)

How to convert a tuple list, after an SQL query, into an array to make operations simple [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
Good morning I have the following query, I make an SQL query and I get a list of tuples in response, the question is that I want to convert that list of tuples into an array to facilitate operations, since after my code requires using the array values to UPDATE in the Database
cursor = db_equipo.cursor()
sql_interface="SELECT id_interface FROM Interface WHERE id_EquipoOrigen_id=%s"
cursor.execute(sql_interface,(id_equipo,))
z=cursor.fetchall()
print(z)
((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))
At first, think about making a loop with two indexes so that you could have an index for the list, and another for the tuple, something like z [x] [y], but it is poorly optimized:
z[0][0]=3027
Z[1][0]=3028
.
.
And I would like something like:
[3027,3028,3029,3030 ...]
You can use a list comprehension:
[datum[0] for datum in z]
Or, if you want your code to be a bit fancy:
next(zip(*z))
You can use a list comprehension:
tup = ((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))
wanted_array = [entry[0] for entry in tup]

Python CodeLab dictionary-traversal [duplicate]

This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 7 years ago.
The question is
This is what I have so far:
dict(nafta_capitals) = canadian_capitals, mexican_capitals, us_capitals
Given three dictionaries, associated with the variables , canadian_capitals, mexican_capitals, and us_capitals, that map provinces or states to their respective capitals, create a new dictionary that combines these three dictionaries, and associate it with a variable , nafta_capitals.
You may need to use defaultdict-
Here nafta is used as key to the three ( canadian_capitals, mexican_capitals, us_capitals) as below-
>>>dic = defaultdict(list)
>>>lst = ['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1', 'nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']
>>>grouped_lst = [lst[i:i+4] for i in range(0,len(lst),4)]
>>>[['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1'], ['nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']]
>>>for i in grouped_lst:dic[i[0]]=i[1:]
>>>dic.items()
>>>[('nafta1', ['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']), ('nafta2', ['canadian_capitals2', 'mexican_capitals2', 'us_capitals2'])]
>>>for i in dic.keys():print dic[i]
>>>['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']
['canadian_capitals2', 'mexican_capitals2', 'us_capitals2']

Transpose block of text [duplicate]

This question already has answers here:
Transposing a text file in Python
(3 answers)
Closed 8 years ago.
I have a block of ones and zeroes, in string:
1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111
I want to transpose it, as if it was a matrix - but keep it in string.
Before I start writing nested for loops, is there an easier way?
s = """1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111"""
>>> [''.join(i) for i in zip(*s.split())]
['1111010000111111',
'1110001110110111',
'1101000110101011',
'1011100010011101',
'1101110001111011',
'1110111000110111',
'0000100000010000',
'0111110000011110',
'0111111001111110',
'0000111100110100',
'1110111110010111',
'1101111111000011',
'1011100100000011',
'1101010111100011',
'1110110110111001',
'1111110000111111']
Edit
If you indeed want a single string as your output, add one more join
>>> '\n'.join(''.join(i) for i in zip(*s.split()))
'1111010000111111\n1110001110110111\n1101000110101011\n1011100010011101\n1101110001111011\n1110111000110111\n0000100000010000\n0111110000011110\n0111111001111110\n0000111100110100\n1110111110010111\n1101111111000011\n1011100100000011\n1101010111100011\n1110110110111001\n1111110000111111'

Categories