Euclidean Distance over 2 dataframes - python
I have 2 Dataframes
DF1-
Name X Y
0 Astonished 0.430 0.890
1 Excited 0.700 0.720
2 Expectant 0.320 0.067
3 Passionate 0.333 0.127
[47 rows * 3 columns]
DF2-
Id X Y
0 1 -0.288453 0.076105
1 4 -0.563453 -0.498895
2 5 -0.788453 -0.673895
3 6 -0.063453 -0.373895
4 7 0.311547 0.376105
[767 rows * 3 columns]
Now what I want to achieve is -
Take the X,Y from first entry from DF2, iterate it over DF1, calculate Euclidean Distance between each value of X,Y in DF2.
Find the minimum of all the Euclidean Distance obtained between the two points, save the minimum result somewhere along with the corresponding entry under the name column.
Example-
Say for any tuple of X,Y in DF2, the minimum Euclidean distance is corresponding to the X,Y value in the row 0 of DF1, then the result should be, the distance and name Astonished.
My Attempt-
import pandas as pd
import numpy as np
import csv
mood = pd.read_csv("C:/Users/Desktop/DF1.csv")
song_value = pd.read_csv("C:/Users/Desktop/DF2.csv")
df_temp = mood.loc[:, ['Arousal','Valence']]
df_temp1 = song_value.loc[:, ['Arousal','Valence']]
import scipy
from scipy import spatial
ary = scipy.spatial.distance.cdist(mood.loc[:, ['Arousal','Valence']], song_value.loc[:, ['Arousal','Valence']], metric='euclidean')
print (ary)
Result Obtained -
[[1.08563344 1.70762362 1.98252253 ... 0.64569366 0.47426051 0.83656989]
[1.17967807 1.75556794 2.03922435 ... 0.59326275 0.2469077 0.79334076]
[0.60852124 1.04915517 1.33326431 ... 0.1848471 0.53293637 0.08394834]
...
[1.26151359 1.5500629 1.81168766 ... 0.74070027 0.70209658 0.75277205]
[0.69085994 1.03764923 1.31608627 ... 0.33265268 0.61928227 0.21397822]
[0.84484398 1.11428893 1.38222899 ... 0.48330291 0.69288125 0.3886008 ]]
I have no clue how I should proceed now.
Please suggest something.
EDIT - 1
I converted the array in another data frame using
new_series = pd.DataFrame(ary)
print (new_series)
Result -
0 1 2 ... 764 765 766
0 1.085633 1.707624 1.982523 ... 0.645694 0.474261 0.836570
1 1.179678 1.755568 2.039224 ... 0.593263 0.246908 0.793341
2 0.608521 1.049155 1.333264 ... 0.184847 0.532936 0.083948
3 0.623534 1.093331 1.378075 ... 0.124156 0.479393 0.109057
4 0.791926 1.352785 1.636748 ... 0.197403 0.245908 0.398619
5 0.740038 1.260768 1.545785 ... 0.092072 0.304926 0.281791
6 0.923284 1.523395 1.803676 ... 0.415540 0.293217 0.611312
7 1.202447 1.679660 1.962823 ... 0.554256 0.247391 0.703298
8 0.824898 1.343684 1.628727 ... 0.177560 0.222666 0.360980
9 1.191411 1.604942 1.883150 ... 0.570771 0.395957 0.668736
10 0.822236 1.456863 1.708469 ... 0.706252 0.787271 0.823542
11 0.741683 1.371996 1.618916 ... 0.704496 0.835235 0.798964
12 0.346244 0.967891 1.240839 ... 0.376504 0.715617 0.359700
13 0.526096 1.163209 1.421820 ... 0.520190 0.748265 0.579333
14 0.435992 0.890291 1.083229 ... 0.937048 1.254437 0.884499
15 0.600338 1.162469 1.375755 ... 0.876228 1.116301 0.891714
16 0.634254 1.059083 1.226407 ... 1.088393 1.373536 1.058550
17 0.712227 1.284502 1.498187 ... 0.917272 1.117806 0.956957
18 0.194387 0.799728 1.045745 ... 0.666713 1.013563 0.597524
19 0.456000 0.708741 0.865870 ... 1.068296 1.420654 0.973234
20 0.633776 0.632060 0.709202 ... 1.277083 1.645173 1.157765
21 0.192291 0.597749 0.826602 ... 0.831713 1.204117 0.716746
22 0.522033 0.526969 0.645998 ... 1.170316 1.546040 1.041762
23 0.668148 0.504480 0.547920 ... 1.316602 1.698041 1.176933
24 0.718440 0.285718 0.280984 ... 1.334008 1.727796 1.166364
25 0.759187 0.265412 0.217165 ... 1.362786 1.757580 1.190132
26 0.598326 0.113459 0.380513 ... 1.087573 1.479296 0.896239
27 0.676841 0.263613 0.474246 ... 1.074911 1.456515 0.875707
28 0.865641 0.365394 0.462742 ... 1.239941 1.612779 1.038790
29 0.463623 0.511737 0.786284 ... 0.719525 1.099122 0.519226
30 0.780386 0.550483 0.750532 ... 0.987863 1.336760 0.788449
31 1.077559 0.711697 0.814205 ... 1.274933 1.602953 1.079529
32 1.020408 0.497152 0.522999 ... 1.372444 1.736938 1.170889
33 0.963911 0.367018 0.336035 ... 1.398444 1.778496 1.198905
34 1.092763 0.759612 0.873457 ... 1.256086 1.574565 1.063570
35 0.903631 0.810449 1.018501 ... 0.921287 1.219046 0.740134
36 0.728728 0.795942 1.045868 ... 0.695317 1.009043 0.512147
37 0.738314 0.600405 0.822742 ... 0.895225 1.239125 0.697393
38 1.206901 1.151385 1.343654 ... 1.064721 1.273002 0.922962
39 1.248530 1.293525 1.508517 ... 0.988508 1.137608 0.880669
40 0.988777 1.205968 1.463036 ... 0.622495 0.776919 0.541414
41 0.941001 1.043940 1.285215 ... 0.732293 0.960420 0.595174
42 1.242508 1.321327 1.544222 ... 0.947970 1.080069 0.851396
43 1.262534 1.399453 1.633948 ... 0.900340 0.989603 0.830024
44 1.261514 1.550063 1.811688 ... 0.740700 0.702097 0.752772
45 0.690860 1.037649 1.316086 ... 0.332653 0.619282 0.213978
46 0.844844 1.114289 1.382229 ... 0.483303 0.692881 0.388601
[47 rows x 767 columns]
Moreover, is this the best approach? Sorry, but am not sure, that's why am putting this up.
Say df_1 and df_2 are your dataframes, first extract your pairs as shown below:
pairs_1 = list(tuple(zip(df_1.X, df_1.Y)))
pairs_2 = list(tuple(zip(df_2.X, df_2.Y)))
Then iterate over pairs as per your use case and get the index of minimum distance for the iterated points:
from scipy import spatial
min_distances = []
closest_pairs = []
names = []
for i in pairs_2:
min_dist = scipy.spatial.distance.cdist([i], pairs_1, metric='euclidean').min()
index_min = scipy.spatial.distance.cdist([i], pairs_1, metric='euclidean').argmin()
min_distances.append(min_dist)
closest_pairs.append(df_1.loc[index_min, ['X', 'Y']])
names.append(df_1.loc[index_min, 'Name'])
Insert results to df_2:
df_2['min_distance'] = min_distances
df_2['closest_pairs'] = [tuple(i.values) for i in closest_pairs]
df_2['name'] = names
df_2
Output:
Id X Y min_distance closest_pairs name
0 1 -0.288453 0.076105 0.608521 (0.32, 0.067) Expectant
1 4 -0.563453 -0.498895 1.049155 (0.32, 0.067) Expectant
2 5 -0.788453 -0.673895 1.333264 (0.32, 0.067) Expectant
3 6 -0.063453 -0.373895 0.584316 (0.32, 0.067) Expectant
4 7 0.311547 0.376105 0.250027 (0.33, 0.127) Passionate
I have added min_distance and closest_pairs as well, you can exclude these columns if you want to.
Related
Subdivide rows into intervals of 50 and take mean
I want to subdivide my dataframe into intervals of 50, then perform the mean of these individual intervals, and then create a new dataframe. 2 3 4 0 20.229517 21.444166 19.528986 1 19.420929 21.029457 18.895041 2 19.214857 21.122784 19.228065 3 19.454653 21.148373 19.249720 4 20.152334 22.183264 20.149488 ... ... ... ... 9995 20.673738 22.252024 21.587578 9996 21.948563 24.904633 23.962317 9997 24.318361 27.220770 25.322933 9998 24.570177 26.371695 23.503048 9999 23.274368 25.145500 22.028172 10000 rows × 3 columns That is a 200 x 3, with the mean values. I would like to do this in pandas if possible. Thx in advance!
You can use integer division by 50 and pass to groupby with aggregate mean: np.random.seed(2020) df = pd.DataFrame(np.random.random(size=(10000, 3))) print (df) 0 1 2 0 0.986277 0.873392 0.509746 1 0.271836 0.336919 0.216954 2 0.276477 0.343316 0.862159 3 0.156700 0.140887 0.757080 4 0.736325 0.355663 0.341093 ... ... ... 9995 0.524481 0.454835 0.108934 9996 0.816516 0.354442 0.224834 9997 0.090518 0.887463 0.444833 9998 0.413673 0.315459 0.691306 9999 0.656559 0.113400 0.063397 [10000 rows x 3 columns] df = df.groupby(df.index // 50).mean() print (df) 0 1 2 0 0.537299 0.484187 0.512674 1 0.446181 0.503547 0.455493 2 0.504955 0.446041 0.464571 3 0.567661 0.494185 0.519785 4 0.485611 0.553636 0.396364 .. ... ... ... 195 0.516285 0.433178 0.526545 196 0.476906 0.474619 0.465957 197 0.497325 0.511659 0.490382 198 0.564468 0.453961 0.467758 199 0.520884 0.455529 0.479706 [200 rows x 3 columns]
Random order of one pandas.DataFrame with respect of other
I have the following structure: data_Cnx = pd.read_csv(path_Connection,sep='\t',header=None) data_Cnx.columns = ["ConnectionID"] data_Srv = pd.read_csv(path_Service,sep='\t',header=None) data_Srv.columns = ["ServiceID"] that can be visualized as the following: print(data_Cnx) ConnectionID 0 CN0120 1 CN0121 2 CN0122 3 CN0123 4 CN0124 ... ... 20 CN0166 21 CN0167 22 CN0168 23 CN0171 24 CN0172 [25 rows x 1 columns] print(data_Srv) ServiceID 0 ST030 1 ST030 2 ST030 3 ST030 4 ST030 ... ... 20 ST040 21 ST040 22 ST040 23 ST050 24 ST050 [25 rows x 1 columns] Literally, each element from data_Cnx corresponds to a parallel element in data_Srv, respecting the order. For instance: CN0120 corresponds to ST030 CN0121 corresponds to ST030 .... CN0166 corresponds to ST040 CN0167 corresponds to ST040 ... CN0171 corresponds to ST050 ... I would like to have another structure or different data_Cnx and data_Srv in which the order of data_Cnx can be randomized, but always in respect of what corresponds in data_Srv. For instance: The data_Cnx and data_Srv can be visualized as the following: print(data_Cnx) ConnectionID 0 CN0120 1 CN0168 2 CN0156 3 CN0133 4 CN0161 ... ... 20 CN0121 21 CN0143 22 CN0127 23 CN0151 24 CN0132 print(data_Srv) [25 rows x 1 columns] ServiceID 0 ST030 1 ST040 2 ST070 3 ST010 4 ST040 ... ... 20 ST030 21 ST050 22 ST030 23 ST070 24 ST010 I was thinking of using randn, but obviously it uses integers as parameters. Do you have an easier idea of how can this be implemented?
I simply found the following works: bigdata = pd.concat([data_Srv,data_Cnx], axis=1) bigdata.sample(n = 20) If someone suggested a better idea, I would be opened to try it :)
Using pd.to_datetime to convert "object" column into %HH:MM:SS
I am doing some exploratory data analysis using finish-time data scraped from the 2018 KONA IRONMAN. I used JSON to format the data and pandas to read into csv. The 'swim','bike','run' columns should be formatted as %HH:MM:SS to be operable, however, I am receiving a ValueError: ('Unknown string format:', '--:--:--'). print(data.head(2)) print(kona.info()) print(kona.describe()) Name div_rank ... bike run 0 Avila, Anthony 2470 138 ... 05:27:59 04:31:56 1 Lindgren, Mikael 1050 151 ... 05:17:51 03:49:20 swim 2472 non-null object bike 2472 non-null object run 2472 non-null object Name div_rank ... bike run count 2472 2472 ... 2472 2472 unique 2472 288 ... 2030 2051 top Jara, Vicente 986 -- ... --:--:-- --:--:-- freq 1 165 ... 122 165 How should I use pd.to_datetime to properly format the 'bike','swim','run' column and for future use sum these columns and append a 'Total Finish Time' column? Thanks!
The reason the error is because it can't pull the time from '--:--:--'. So you'd need to convert all those to '00:00:00', but then that implies they did the event in 0 time. The other option is to just convert the times that are present, leaving a null in the places that don't have a time. This will also include a date of 1900-01-01, when you convert to datetime. I put the .dt.time so only time will display. timed_events = ['bike', 'swim', 'run'] for event in timed_events: result[event] = pd.to_datetime(result[result[event] != '--:--:--'][event], format="%H:%M:%S").dt.time The problem with this though is I remember seeing you wanted to sum those times, which would require you to do some extra conversions. So I'm suggesting to use .to_timedelta() instead. It'll work the same way, as you'd need to not include the --:--:--. But then you can sum those times. I also added a column of number of event completed, so that if you want to sort by best times, you can filter out anyone who hasn't competed in all three events, as obviously they'd have better times because they are missing entire events: I'll also add, regarding the comment of: "You think providing all the code will be helpful but it does not. You will get a quicker and more useful response if you keep the code minimum that can replicate your issue.stackoverflow.com/help/mcve – mad_ " I'll give him the benefit of the doubt as seeing the whole code and not realizing that the code you provided was the minimal code to replicate your issue, as no one wants to code a way to generate your data to work with. Sometimes you can explicitly state that in your question. ie: Here's the code to generate my data: CODE PART 1 import bs4 import pandas as pd code... But now that I have the data, here's where I'm having trouble: df = pd.to_timedelta()... ... Luckily I remembered helping you earlier on this so knew I could go back and get that code. So the code you originally had was fine. But here's the full code I used, which is a different way of storing the csv than you orginially had. So you can change that part, but the end part is what you'll need: from bs4 import BeautifulSoup, Comment from collections import defaultdict import requests import pandas as pd sauce = 'http://m.ironman.com/triathlon/events/americas/ironman/world-championship/results.aspx' r = requests.get(sauce) data = r.text soup = BeautifulSoup(data, 'html.parser') def parse_table(soup): result = defaultdict(list) my_table = soup.find('tbody') for node in my_table.children: if isinstance(node, Comment): # Get content and strip comment "<!--" and "-->" # Wrap the rows in "table" tags as well. data = '<table>{}</table>'.format(node[4:-3]) break table = BeautifulSoup(data, 'html.parser') for row in table.find_all('tr'): name, _, swim, bike, run, div_rank, gender_rank, overall_rank = [col.text.strip() for col in row.find_all('td')[1:]] result[name].append({ 'div_rank': div_rank, 'gender_rank': gender_rank, 'overall_rank': overall_rank, 'swim': swim, 'bike': bike, 'run': run, }) return result jsonObj = parse_table(soup) result = pd.DataFrame() for k, v in jsonObj.items(): temp_df = pd.DataFrame.from_dict(v) temp_df['name'] = k result = result.append(temp_df) result = result.reset_index(drop=True) result.to_csv('C:/data.csv', index=False) # However you read in your csv/dataframe, use the code below on it to get those times timed_events = ['bike', 'swim', 'run'] for event in timed_events: result[event] = pd.to_timedelta(result[result[event] != '--:--:--'][event]) result['total_events_participated'] = 3 - result.isnull().sum(axis=1) result['total_times'] = result[timed_events].sum(axis=1) Output: print (result) bike div_rank ... total_events_participated total_times 0 05:27:59 138 ... 3 11:20:06 1 05:17:51 151 ... 3 10:16:17 2 06:14:45 229 ... 3 14:48:28 3 05:13:56 162 ... 3 10:19:03 4 05:19:10 6 ... 3 09:51:48 5 04:32:26 25 ... 3 08:23:26 6 04:49:08 155 ... 3 10:16:16 7 04:50:10 216 ... 3 10:55:47 8 06:45:57 71 ... 3 13:50:28 9 05:24:33 178 ... 3 10:21:35 10 06:36:36 17 ... 3 14:36:59 11 NaT -- ... 0 00:00:00 12 04:55:29 100 ... 3 09:28:53 13 05:39:18 72 ... 3 11:44:40 14 04:40:41 -- ... 2 05:35:18 15 05:23:18 45 ... 3 10:55:27 16 05:15:10 3 ... 3 10:28:37 17 06:15:59 78 ... 3 11:47:24 18 NaT -- ... 0 00:00:00 19 07:11:19 69 ... 3 15:39:51 20 05:49:02 29 ... 3 10:32:36 21 06:45:48 4 ... 3 13:39:17 22 04:39:46 -- ... 2 05:48:38 23 06:03:01 3 ... 3 11:57:42 24 06:24:58 193 ... 3 13:52:57 25 05:07:42 116 ... 3 10:01:24 26 04:44:46 112 ... 3 09:29:22 27 04:46:06 55 ... 3 09:32:43 28 04:41:05 69 ... 3 09:31:32 29 05:27:55 68 ... 3 11:09:37 ... ... ... ... ... 2442 NaT -- ... 0 00:00:00 2443 05:26:40 3 ... 3 11:28:53 2444 05:04:37 19 ... 3 10:27:13 2445 04:50:45 74 ... 3 09:15:14 2446 07:17:40 120 ... 3 14:46:05 2447 05:26:32 45 ... 3 10:50:48 2448 05:11:26 186 ... 3 10:26:00 2449 06:54:15 185 ... 3 14:05:16 2450 05:12:10 22 ... 3 11:21:37 2451 04:59:44 45 ... 3 09:29:43 2452 06:03:59 96 ... 3 12:12:35 2453 06:07:27 16 ... 3 12:47:11 2454 04:38:06 91 ... 3 09:52:27 2455 04:41:56 14 ... 3 08:58:46 2456 04:38:48 85 ... 3 09:18:31 2457 04:42:30 42 ... 3 09:07:29 2458 04:40:54 110 ... 3 09:32:34 2459 06:08:59 37 ... 3 12:15:23 2460 04:32:20 -- ... 2 05:31:05 2461 04:45:03 96 ... 3 09:30:06 2462 06:14:29 95 ... 3 13:38:54 2463 06:00:20 164 ... 3 12:10:03 2464 05:11:07 22 ... 3 10:32:35 2465 05:56:06 188 ... 3 13:32:48 2466 05:09:26 2 ... 3 09:54:55 2467 05:22:15 7 ... 3 10:26:14 2468 05:53:14 254 ... 3 12:34:21 2469 05:00:29 156 ... 3 10:18:29 2470 04:30:46 7 ... 3 08:38:23 2471 04:34:59 39 ... 3 09:04:13 [2472 rows x 9 columns]
Binning a data set using Pandas
Given a csv file of... neg,,,,,,, SAMPLE 1,,SAMPLE 2,,SAMPLE 3,,SAMPLE 4, 50.0261,2.17E+02,50.0224,3.31E+02,50.0007,5.38E+02,50.0199,2.39E+02 50.1057,2.65E+02,50.0435,3.92E+02,50.0657,5.52E+02,50.0465,3.37E+02 50.1514,2.90E+02,50.0781,3.88E+02,50.1115,5.75E+02,50.0584,2.58E+02 50.166,3.85E+02,50.1245,4.25E+02,50.1258,5.11E+02,50.0765,4.47E+02 50.1831,2.55E+02,50.1748,3.71E+02,50.1411,6.21E+02,50.1246,1.43E+02 50.2023,3.45E+02,50.2161,2.59E+02,50.1671,5.56E+02,50.1866,3.77E+02 50.223,4.02E+02,50.2381,4.33E+02,50.1968,6.31E+02,50.2276,3.41E+02 50.2631,1.89E+02,50.2826,4.63E+02,50.211,3.92E+02,50.2717,4.71E+02 50.2922,2.72E+02,50.3593,4.52E+02,50.2279,5.92E+02,50.376,3.09E+02 50.319,2.46E+02,50.4019,4.15E+02,50.2929,5.60E+02,50.3979,2.56E+02 50.3523,3.57E+02,50.423,3.31E+02,50.3659,4.84E+02,50.4237,3.28E+02 50.3968,4.67E+02,50.4402,1.76E+02,50.437,1.89E+02,50.4504,2.71E+02 50.4431,1.88E+02,50.479,4.85E+02,50.5137,6.63E+02,50.5078,2.54E+02 50.481,3.63E+02,50.5448,3.51E+02,50.5401,5.11E+02,50.5436,2.69E+02 50.506,3.73E+02,50.5872,4.03E+02,50.5593,6.56E+02,50.555,3.06E+02 50.5379,3.00E+02,50.6076,2.96E+02,50.6034,5.02E+02,50.6059,2.83E+02 50.5905,2.38E+02,50.6341,2.67E+02,50.6579,6.37E+02,50.6484,1.99E+02 50.6564,1.30E+02,50.662,3.53E+02,50.6888,7.37E+02,50.7945,4.84E+02 50.7428,2.38E+02,50.6952,4.21E+02,50.7132,6.71E+02,50.8044,4.41E+02 50.8052,3.67E+02,50.7397,1.99E+02,50.7421,6.29E+02,50.8213,1.69E+02 50.8459,2.80E+02,50.7685,3.73E+02,50.7872,5.30E+02,50.8401,3.88E+02 50.9021,3.56E+02,50.7757,4.54E+02,50.8251,4.13E+02,50.8472,3.61E+02 50.9425,3.89E+02,50.8027,7.20E+02,50.8418,5.73E+02,50.8893,1.18E+02 51.0117,2.29E+02,50.8206,2.93E+02,50.8775,4.34E+02,50.9285,2.64E+02 51.0244,5.19E+02,50.8364,4.80E+02,50.9101,4.25E+02,50.9591,1.64E+02 51.0319,3.62E+02,50.8619,2.90E+02,50.9222,5.11E+02,51.0034,2.70E+02 51.0439,4.24E+02,50.9098,3.22E+02,50.9675,4.33E+02,51.0577,2.88E+02 51.0961,3.59E+02,50.969,3.87E+02,51.0123,6.03E+02,51.0712,3.18E+02 51.1429,2.49E+02,51.0009,2.42E+02,51.0266,7.30E+02,51.1015,1.84E+02 51.1597,2.71E+02,51.0262,1.32E+02,51.0554,3.69E+02,51.1291,3.71E+02 51.177,2.84E+02,51.0778,1.58E+02,51.1113,4.50E+02,51.1378,3.54E+02 51.1924,2.00E+02,51.1313,4.07E+02,51.1464,3.86E+02,51.1871,1.55E+02 51.2055,2.25E+02,51.1844,2.08E+02,51.1826,7.06E+02,51.2511,2.05E+02 51.2302,3.81E+02,51.2197,5.49E+02,51.2284,7.00E+02,51.3036,2.60E+02 51.264,2.16E+02,51.2306,3.76E+02,51.271,3.83E+02,51.3432,1.99E+02 51.2919,2.29E+02,51.2468,2.87E+02,51.308,3.89E+02,51.3775,2.45E+02 51.3338,3.67E+02,51.2739,5.56E+02,51.3394,5.17E+02,51.3977,3.86E+02 51.3743,2.57E+02,51.3228,3.18E+02,51.3619,6.03E+02,51.4151,3.37E+02 51.3906,3.78E+02,51.3685,2.33E+02,51.3844,4.44E+02,51.4254,2.72E+02 51.4112,3.29E+02,51.3912,5.03E+02,51.4179,5.68E+02,51.4426,3.17E+02 51.4423,1.86E+02,51.4165,2.68E+02,51.4584,5.10E+02,51.4834,3.87E+02 51.537,3.48E+02,51.4645,3.76E+02,51.5179,5.75E+02,51.544,4.37E+02 51.637,4.51E+02,51.5078,2.76E+02,51.569,4.73E+02,51.5554,4.52E+02 51.665,2.27E+02,51.5388,2.51E+02,51.5894,4.57E+02,51.5958,1.96E+02 51.6925,5.60E+02,51.5486,2.79E+02,51.614,4.88E+02,51.6329,5.40E+02 51.7409,4.19E+02,51.5584,2.53E+02,51.6458,5.72E+02,51.6477,3.23E+02 51.7851,4.29E+02,51.5961,2.72E+02,51.7076,4.36E+02,51.6577,2.70E+02 51.8176,3.11E+02,51.6608,2.04E+02,51.776,5.59E+02,51.6699,3.89E+02 51.8764,3.94E+02,51.7093,5.14E+02,51.8157,6.66E+02,51.6788,2.83E+02 51.9135,3.26E+02,51.7396,1.88E+02,51.8514,4.26E+02,51.7201,3.91E+02 51.9592,2.66E+02,51.7931,2.72E+02,51.8791,5.61E+02,51.7546,3.41E+02 51.9954,2.97E+02,51.8428,5.96E+02,51.9129,5.14E+02,51.7646,2.27E+02 52.0751,2.24E+02,51.8923,3.94E+02,51.959,5.18E+02,51.7801,1.43E+02 52.1456,3.26E+02,51.9177,2.82E+02,52.0116,4.21E+02,51.8022,2.27E+02 52.1846,3.42E+02,51.9265,3.21E+02,52.0848,5.10E+02,51.83,2.66E+02 52.2284,2.66E+02,51.9413,3.56E+02,52.1412,6.20E+02,51.8698,1.74E+02 52.2666,5.32E+02,51.9616,2.19E+02,52.1722,5.72E+02,51.9084,2.89E+02 52.2936,4.24E+02,51.9845,1.53E+02,52.1821,5.18E+02,51.937,1.69E+02 52.3256,3.69E+02,52.0051,3.53E+02,52.2473,5.51E+02,51.9641,3.31E+02 52.3566,2.50E+02,52.0299,2.87E+02,52.3103,4.12E+02,52.0292,2.63E+02 52.4192,3.08E+02,52.0603,3.15E+02,52.35,8.76E+02,52.0633,3.94E+02 52.4757,2.99E+02,52.0988,3.45E+02,52.3807,6.95E+02,52.0797,2.88E+02 52.498,2.37E+02,52.1176,3.63E+02,52.4234,4.89E+02,52.1073,2.97E+02 52.57,2.58E+02,52.1698,3.11E+02,52.4451,4.54E+02,52.1546,3.41E+02 52.6178,4.29E+02,52.2352,3.96E+02,52.4627,5.38E+02,52.2219,3.68E+02 How can one split the samples using overlapping bins of 0.25 m/z - where the first column of each tuple (Sample n,,) contains a m/z value and the second containing the weight? To load the file into a Pandas DataFrame I currently do: import csv, pandas as pd def load_raw_data(): raw_data = [] with open("negsmaller.csv", "rb") as rawfile: reader = csv.reader(rawfile, delimiter=",") next(reader) for row in reader: raw_data.append(row) raw_data = pd.DataFrame(raw_data) return raw_data.T if __name__ == '__main__': raw_data = load_raw_data() print raw_data Which returns 0 1 2 3 4 5 6 \ 0 SAMPLE 1 50.0261 50.1057 50.1514 50.166 50.1831 50.2023 1 2.17E+02 2.65E+02 2.90E+02 3.85E+02 2.55E+02 3.45E+02 2 SAMPLE 2 50.0224 50.0435 50.0781 50.1245 50.1748 50.2161 3 3.31E+02 3.92E+02 3.88E+02 4.25E+02 3.71E+02 2.59E+02 4 SAMPLE 3 50.0007 50.0657 50.1115 50.1258 50.1411 50.1671 5 5.38E+02 5.52E+02 5.75E+02 5.11E+02 6.21E+02 5.56E+02 6 SAMPLE 4 50.0199 50.0465 50.0584 50.0765 50.1246 50.1866 7 2.39E+02 3.37E+02 2.58E+02 4.47E+02 1.43E+02 3.77E+02 7 8 9 ... 56 57 58 \ 0 50.223 50.2631 50.2922 ... 52.2284 52.2666 52.2936 1 4.02E+02 1.89E+02 2.72E+02 ... 2.66E+02 5.32E+02 4.24E+02 2 50.2381 50.2826 50.3593 ... 51.9413 51.9616 51.9845 3 4.33E+02 4.63E+02 4.52E+02 ... 3.56E+02 2.19E+02 1.53E+02 4 50.1968 50.211 50.2279 ... 52.1412 52.1722 52.1821 5 6.31E+02 3.92E+02 5.92E+02 ... 6.20E+02 5.72E+02 5.18E+02 6 50.2276 50.2717 50.376 ... 51.8698 51.9084 51.937 7 3.41E+02 4.71E+02 3.09E+02 ... 1.74E+02 2.89E+02 1.69E+02 59 60 61 62 63 64 65 0 52.3256 52.3566 52.4192 52.4757 52.498 52.57 52.6178 1 3.69E+02 2.50E+02 3.08E+02 2.99E+02 2.37E+02 2.58E+02 4.29E+02 2 52.0051 52.0299 52.0603 52.0988 52.1176 52.1698 52.2352 3 3.53E+02 2.87E+02 3.15E+02 3.45E+02 3.63E+02 3.11E+02 3.96E+02 4 52.2473 52.3103 52.35 52.3807 52.4234 52.4451 52.4627 5 5.51E+02 4.12E+02 8.76E+02 6.95E+02 4.89E+02 4.54E+02 5.38E+02 6 51.9641 52.0292 52.0633 52.0797 52.1073 52.1546 52.2219 7 3.31E+02 2.63E+02 3.94E+02 2.88E+02 2.97E+02 3.41E+02 3.68E+02 [8 rows x 66 columns] Process finished with exit code 0 My desired output: To take the overlapping 0.25 bins and then take the average of the column next to it and have it as one. So, 0.01 3 0.10 4 0.24 2 would become .25 3
How to add a repeated column using pandas
I am doing my homework and I encounter a problem, I have a large matrix, the first column Y002 is a nominal variable, which has 3 levels and encoded as 1,2,3 respectively. The other two columns V96 and V97 are just numeric. Now, I wanna get a group mean corresponds to the variable Y002. I wrote the code like this group = data2.groupby(by=["Y002"]).mean() Then I index to get each group mean using group1 = group["V96"] group2 = group["V97"] Now I wanna append this group mean as a new column into the original dataframe, in which each mean matches the corresponding Y002 code(1 or 2 or 3). Actually I tried this code, but it only shows NAN. data2["group1"] = pd.Series(group1, index=data2.index) Hope someone could help me with this, many thanks :) PS: Hope this makes sense. just like R language, we can do the same thing using data2$group1 = with(data2, tapply(V97,Y002,mean))[data2$Y002] But how can we implement this in Python and pandas???
You can use .transform() import pandas as pd import numpy as np # your data # ============================ np.random.seed(0) df = pd.DataFrame({'Y002': np.random.randint(1,4,100), 'V96': np.random.randn(100), 'V97': np.random.randn(100)}) print(df) V96 V97 Y002 0 -0.6866 -0.1478 1 1 0.0149 1.6838 2 2 -0.3757 0.9718 1 3 -0.0382 1.6077 2 4 0.3680 -0.2571 2 5 -0.0447 1.8098 3 6 -0.3024 0.8923 1 7 -2.2244 -0.0966 3 8 0.7240 -0.3772 1 9 0.3590 -0.5053 1 .. ... ... ... 90 -0.6906 1.5567 2 91 -0.6815 -0.4189 3 92 -1.5122 -0.4097 1 93 2.1969 1.1164 2 94 1.0412 -0.2510 3 95 -0.0332 -0.4152 1 96 0.0656 -0.6391 3 97 0.2658 2.4978 1 98 1.1518 -3.0051 2 99 0.1380 -0.8740 3 # processing # =========================== df['V96_mean'] = df.groupby('Y002')['V96'].transform(np.mean) df['V97_mean'] = df.groupby('Y002')['V97'].transform(np.mean) df V96 V97 Y002 V96_mean V97_mean 0 -0.6866 -0.1478 1 -0.1944 0.0837 1 0.0149 1.6838 2 0.0497 -0.0496 2 -0.3757 0.9718 1 -0.1944 0.0837 3 -0.0382 1.6077 2 0.0497 -0.0496 4 0.3680 -0.2571 2 0.0497 -0.0496 5 -0.0447 1.8098 3 0.0053 -0.0707 6 -0.3024 0.8923 1 -0.1944 0.0837 7 -2.2244 -0.0966 3 0.0053 -0.0707 8 0.7240 -0.3772 1 -0.1944 0.0837 9 0.3590 -0.5053 1 -0.1944 0.0837 .. ... ... ... ... ... 90 -0.6906 1.5567 2 0.0497 -0.0496 91 -0.6815 -0.4189 3 0.0053 -0.0707 92 -1.5122 -0.4097 1 -0.1944 0.0837 93 2.1969 1.1164 2 0.0497 -0.0496 94 1.0412 -0.2510 3 0.0053 -0.0707 95 -0.0332 -0.4152 1 -0.1944 0.0837 96 0.0656 -0.6391 3 0.0053 -0.0707 97 0.2658 2.4978 1 -0.1944 0.0837 98 1.1518 -3.0051 2 0.0497 -0.0496 99 0.1380 -0.8740 3 0.0053 -0.0707 [100 rows x 5 columns]