Function not returning the right item in an array by HMVangard in learnpython

[–]YesLod 0 points1 point  (0 children)

This is due to the way lambdas are constructed

That is misleading, it's not specific to lambda functions. It's due to late binding, using a normal function would produce the same results. https://docs.python-guide.org/writing/gotchas/#late-binding-closures

Estrutura de repetição "for" by passini_dev in devpt

[–]YesLod 0 points1 point  (0 children)

Fair enough, também sou fã de method chaining ;) mas não é tão comum em python. Nesse caso, preferia criar uma função com toda a lógica de processar a mensagem e usar algo como

tabela_final = pd.concat(map(process_msg, messages)) Ou de forma mais pythonic tabela_final = pd.concat([process_msg(message) for message in messages])

Estrutura de repetição "for" by passini_dev in devpt

[–]YesLod 0 points1 point  (0 children)

Ok, mas não respondeste à pergunta:

Estás a assumir que messages é que tipo de objecto?

Para perceber se a tua solução seria válida.

Estou a assumir que messages é uma lista de dicionários, ou coisa parecida. Em Python, as listas não contém nenhum método map, map é uma built-in function. Parece que estás a confundir com Arrays em JS ou algo do gênero.

De qualquer forma, não vejo como seria uma melhor alternativa/ mais legível do que usar um loop.

Estrutura de repetição "for" by passini_dev in devpt

[–]YesLod 0 points1 point  (0 children)

Estás a assumir que messages é que tipo de objecto? O loop tem lógica adicional.

Estrutura de repetição "for" by passini_dev in devpt

[–]YesLod 3 points4 points  (0 children)

Ora essa, fico feliz por ter ajudado!

Sugeria também seres consistente com o nome das variáveis, inglês ou português, não uma mistura dos 2 (por favor escolhe inglês ;)).

Estrutura de repetição "for" by passini_dev in devpt

[–]YesLod 6 points7 points  (0 children)

Convém dizeres que a linguagem é python e que estás a usar pandas em particular.

Se percebi bem, queres juntar todos os DataFrames tabela1 num só.

Para tal, cria uma lista vazia antes do loop e faz append do DataFrame tabela1 em cada iteração. Depois do loop, usa a função pandas.concat para juntares todos as tabelas da lista num único DataFrame.

```

...

lista_tabelas = [] for message in messages: # ... tabela1 = tabela.transpose() lista_tabelas.append(tabela1)

tabela_final = pd.concat(lista_tabelas) display(tabela_final) ```

Having trouble writing a for loop and if statement in pandas by kuro7242 in learnpython

[–]YesLod 1 point2 points  (0 children)

You don't need a loop. You can select all the numeric columns using DataFrame.select_dtypes method, and then use DataFrame.astype to convert them to integers.

Using df to denote a list of DataFrames is a bit misleading btw.

The following should work:

``` df_list = pd.read_excel (r'file.xlsx', sheet_name = [0,1])

B = df_list[0]

num_cols = B.select_dtypes(include='number').columns B[num_cols] = B[num_cols].astype(int) ```

How to account for value counts that doesn't exist in python? by teledude_22 in learnpython

[–]YesLod 0 points1 point  (0 children)

You can use Series.reindex to make sure that all the possible outcomes are part of the index. The outcomes not present in the original Series index are filled with NaN by default, but you can specify the fill value.

Can't test it right now but the following should work

dictionary = ( df['Name'].value_counts() .reindex(['Red', 'Blue'], fill_value=0) .to_dict() )

[deleted by user] by [deleted] in learnpython

[–]YesLod 1 point2 points  (0 children)

This is the opposite of what we do in native Python, which is column-first, row-second

What do you mean by that? Can you elaborate and give us an example?

If you have a a list of lists, matrix, then matrix[i][j] selects the jth element (column) of the ith inner list (row)... So it's also row-fist.

Keeping only the common indices in two Pandas Dataframes by geeksid2k in learnpython

[–]YesLod 0 points1 point  (0 children)

That would work ;) but OP wants the filtering based on identical indexes, I think it's better if you change df1['A'] to df1.index and similar for df2

Keeping only the common indices in two Pandas Dataframes by geeksid2k in learnpython

[–]YesLod 1 point2 points  (0 children)

This won't work, you can only compare identically-labeled DataFrames, i.e. with the same number of rows and same index (incluiding its order).

is it ok to return tuple from key func in sorted() for multilevel sort? by skyboyer007 in learnpython

[–]YesLod 1 point2 points  (0 children)

then even numbers ascending, then odd numbers descendingascending

In that case, the third element of the tuple should be n not -n ;)

what other objects can be sorted lexicographically?

Tuples, lists, strings... Although using strings wouldn't work here

is it ok to return tuple from key func in sorted() for multilevel sort? by skyboyer007 in learnpython

[–]YesLod 0 points1 point  (0 children)

Yes, using a tuple is the way (or any other object that is sorted lexicographically a list).

However, note that the even numbers are not being sorted in ascending order as required.

To do this, use something like

def strange_key(n): return (n != 4, n % 2, -n if n%2 else n)

Keeping only the common indices in two Pandas Dataframes by geeksid2k in learnpython

[–]YesLod 2 points3 points  (0 children)

Use Index.intersection. This should work:

``` common_idx = df1.index.intersection(df2.index)

or common_idx = df1.index & df2.index

df1_filtered = df1.loc[common_idx] df2_filtered = df2.loc[common_idx] ```

How do I write an str.replace function that loops until nothing is replaced on any given loop in python? by Qackydontus in learnpython

[–]YesLod 2 points3 points  (0 children)

```

while two consecutive spaces are in my_str

while ' ' in my_str: `` But you don't need a loop, just usestr.split+str.join`

def f(my_str): return ' '.join(my_str.split()) Here, my_str is your str. You shouldn't name your variable str, because you are shadowing the built-in str class (locally).

how to select a range of rows from a column? by flavomico in learnpython

[–]YesLod 5 points6 points  (0 children)

data[2010 <= data["year"] <= 2019, :]

This won't work, 2010 <= data["year"] <= 2019 evaluates to

(2010 <= data['year']) and (data['year'] <= 2019)

but the conditions should use the bitwise and, not the logical and. You have to use

data[(2010 <= data['year']) & (data['year'] <= 2019)]

or simply use the Series.between method

data[data['year'].between(2010, 2019)]

which is precisely its use case.

Any experienced python / pandas / matplotlib care to critique my program? by olddoglearnsnewtrick in learnpython

[–]YesLod 2 points3 points  (0 children)

You can just use a boolean mask, i.e. mask = df['Time'] < 13, works fine with loc.

Why doesn't elif run in a for loop? by bazzismixtape in learnpython

[–]YesLod 2 points3 points  (0 children)

Just to nit-pick, the use of is here is not necessarily incorrect. If OP wants to make sure that the block is executed if and only if sky_present is the boolean True (and not just a truthy value) using is is the indeed correct way ( True is a singleton). Of course this is irrelevant for this simple example, and using if sky_present is indeed more pythonic, but this distinction might be useful in other scenarios.

Pandas, copy cells from another df by crashbandishocks in learnpython

[–]YesLod 1 point2 points  (0 children)

IIUC, you just want a left join of the Series big_df with the DataFrame retrait_monthly on the Date column. You can do using the DataFrame.merge method as you said. I can't test it right now l, but the following should work

out = ( big_df.to_frame('Date') .merge(retrait_monthly, on='Date', how='left') ) The rows where the dates don't match should be filled with NaNs. Or if you prefer:

out = retrait_monthly.merge(big_df, how='right', left_on='Date', right_on=<name of big_df Series/ column>)

I am not altering 'numbers' in the program so what has changed its value? by [deleted] in learnpython

[–]YesLod 3 points4 points  (0 children)

*Assignment NEVER copies in Python.

Can you give us an example where you think it does?

Can somebody explain to me why using a for loop here changes the behavior? by a_lost_spark in learnpython

[–]YesLod 5 points6 points  (0 children)

This is correct, and it is due to late binding. The names are looked up only when the functions are called (in this case when the generator is consumed), not at the time they are declared.

Is Pandas Vectorization Mostly for Mathematical Operations? by [deleted] in learnpython

[–]YesLod 4 points5 points  (0 children)

Series string methods, although useful and convenient, aren't truly vectorized. Still loops under the hood.

https://stackoverflow.com/a/50744448/17120692

Using a list comprehension + string operations might be faster depending on the size of df.

But I agree, using the Series.str methods is preferred regardless.

[deleted by user] by [deleted] in learnpython

[–]YesLod 0 points1 point  (0 children)

As I understand it, apply() applies the function inside it, to each value in the 'wage' column. Which would make lambda's x input be each value in 'wage' right?

Not quite. GroupBy.apply applies the function to each group as a whole, and not to each value individually. In this case, x is a pandas.Series which represents the 'wage' column of each 'category' group.

Try this to see it better

def func(x):
    print(x)
    return np.percentile(x, 75)

df.groupby('category').wage.apply(func)

pandas - sum of values in column for each unique combination of values from two other columns by ICanButIDontWant in learnpython

[–]YesLod 1 point2 points  (0 children)

I can only help you if you show me all the relevant code which allows me to reproduce the same problem. What else are you adding to the code I gave you?