Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 123200

Pandas is a Python data analysis library.

3 votes
Accepted

Best practice for cleaning Pandas dataframe columns

This is not too bad. It's a good thing you use keyword arguments for the replace method I always try to keep my original data in its original state, and continue with the cleaned dataframe. fluent sty …
Maarten Fabré's user avatar
4 votes

Reduce repetition in Pandas DataFrame column assignments

Try to get a good book or tutorial about pandas. …
Maarten Fabré's user avatar
1 vote

Pivot some rows to new columns in DataFrame

groupby When iterating over the values in a column, it is bad practice to hardcode the values (for pivot in [1, 2, 3]). A better way would have been for pivot in df["dof"].unique(), but the best way …
Maarten Fabré's user avatar
2 votes

Concordance index calculation

You will not get dramatic speedups untill you can vectorize the operations, but here are some tips already indexing before iterating instead of for i, row in df.iterrows(): if row['event'] == …
Maarten Fabré's user avatar
2 votes
Accepted

Pivot DataFrame with DateTimeIndex

Review all in all, this code is rather clean. I would use a generator comprehension and itertools.chain in fake_disrete_data instead of the nested for-loop, but that is a matter of taste linewraps …
Maarten Fabré's user avatar
2 votes
Accepted

Iterating through, and editing a dataframe, using outputs from a collision detector's neighb...

= cluster] if changed_indices.empty: clusters[indices] = indices.min() else: clusters[indices] = changed_indices.min() This makes use of the pandas indexing. … This method can also be easily tested in isolation groupby Instead of iterating over the unique values, pandas has its groupby functionality. …
Maarten Fabré's user avatar
2 votes
Accepted

Linear Regression on Pandas

np.random.randint(1000, 2000, size=size), date_label: pd.DatetimeIndex(start=start, freq=freq, periods=size), } ) summarize The rolling mean and std you do can be done with builtin pandas
Maarten Fabré's user avatar
1 vote

speed up/optimize pandas code

= np.array( [np.roll(arr, -i) for i in range(l)], copy=False, ) / arr - mask return pd.DataFrame(data = result.T, index = data.index) I find this code less clear than the pandas
Maarten Fabré's user avatar
4 votes
Accepted

Get minimum of each group based on hour criteria using pandas

leave the original data intact Since df is the original data, adding columns to it can have a strange side effect in another part of the calculation. Best is not to touch the original data, and do al …
Maarten Fabré's user avatar
0 votes

Alcohol consumption project

drink: { continent: data.loc[ data[drink].nlargest(5).index, ["country", drink] ] for continent, data in data_by_continent.items() } for drink in drinks } pandas
Maarten Fabré's user avatar
2 votes

I made a Python program to calculate price based on Inflation Rate

Here you can use pandas further. For starters, you can make the year a pandas.Period, so you can index the year immediately. Then you can do the division by 100 already. … Since pandas has a nice cumprod function, you can already add 1 to each index def get_inflation_rate( filename: typing.Union[Path, str, typing.IO] ) -> pd.Series: """Read the inflation data from …
Maarten Fabré's user avatar
5 votes
Accepted

Subtable timestamps in python pandas

you can use groupby.transform df["next_timestamp"] = df.groupby("id")["time_stamp"].transform( lambda x: x.shift(-1) )
Maarten Fabré's user avatar
1 vote
Accepted

Display the difference between DataFrames' dtypes?

self Why put this method on a class? the lack of use of self in the method should act as a flag string result you format the mismatch as a string (f"df1:{val}, df2: {df2.dtypes[key]}"). This way yo …
Maarten Fabré's user avatar
1 vote
Accepted

Python Pandas - finding duplicate names and telling them apart

list) for i in df2.index: nameid[df2.loc[i, 'Name']].append(df2.loc[i, 'People ID']) would work iteration apart from the fact that you want to prevent iteration as much as possible when using pandas … namead = { name for name, ids in nameid.items() if len(ids) > 1 } pandas indexing dupes = [i for i in df2.index if df2.loc[i, 'Name'] in namead.keys()] for i in duperevs: df2.loc[i, …
Maarten Fabré's user avatar
6 votes
Accepted

Pandas add calculated row for every row in a dataframe

iterrows Of all the ways to iterate over a pandas DataFrame, iterrows is the worst. …
Maarten Fabré's user avatar

15 30 50 per page