converting pandas data frame to dictionary of tuples [duplicate] - python

This question already has answers here:
pandas DataFrame to dict with values as tuples
(2 answers)
Closed 5 years ago.
I am using the following code to convert a Dataframe whose structure is as follows
dummy= df.set_index(['location']).T.to_dict('list')
for key,value in dummy.items():
dummy[key] = tuple(value)
to obtain a dictionary of tuples
{loc_1:(35.99,-81.44),loc_2:(22.55,-108.5)}
Question
1. Will the order be preserved as lat-long? (Is there a chance the first tuple can turn out to be (-81.44,35.99)?
Question 2. Is there a better(faster/elegant)way of doing the above

Using a comprehension and itertuples
dict([(t.location, (t.lat, t.long)) for t in df.itertuples()])
{loc_1: (35.99, -81.44), loc_2: (22.55, -108.5)}

Related

Sort rows of a 2d numpy array in ascending order [duplicate]

This question already has answers here:
Python Numpy Sort rows [duplicate]
(2 answers)
Closed 2 years ago.
I have an numpy array arr with shape (1500,10) where each element is a digit from 0 to 9. I'd like to sort the array as each row's elements are concatenated to form a single number and then sort these numbers in ascending order.Let a simple array be like:
arr = ([[3,4,1,5,1,2,3,4,5,6],
[1,2,3,5,6,2,9,2,1,2],
[0,3,1,4,2,1,6,8,2,1],
[0,1,3,5,1,2,9,2,1,7],
[2,3,5,7,1,2,5,7,1,5]])
it should return
arr = ([[0,1,3,5,1,2,9,2,1,7],
[0,3,1,4,2,1,6,8,2,1],
[1,2,3,5,6,2,9,2,1,2],
[2,3,5,7,1,2,5,7,1,5],
[3,4,1,5,1,2,3,4,5,6]])
You can do the following:
arr[np.lexsort(np.flip(arr.transpose(), axis=0))]

How to get the second largest value in Pandas Python [duplicate]

This question already has answers here:
Get first and second highest values in pandas columns
(7 answers)
Closed 4 years ago.
This is my code:
maxData = all_data.groupby(['Id'])[features].agg('max')
all_data = pd.merge(all_data, maxData.reset_index(), suffixes=["", "_max"], how='left', on=['Id'])
Now Instead of getting the max value, How can I fetch the second max value in the above code (groupBy Id)
Try using nlargest
maxData = all_data.groupby(['Id'])[features].apply(lambda x:x.nlargest(2)[1]).reset_index(drop=True)
You can use the nth method just after sorting the values;
maxData = all_data.sort_values("features", ascending=False).groupby(['Id']).nth(1)
Please ignore apply method as it decreases performance of code.

Python: len() of unknown numpy array column length [duplicate]

This question already has answers here:
Counting the number of non-NaN elements in a numpy ndarray in Python
(5 answers)
Closed 4 years ago.
I'm currently trying to learn Python and Numpy. The task is to determine the length of individual columns of an imported CSV file.
So far I have:
import numpy as np
data = np.loadtxt("assignment5_data.csv", delimiter = ',')
print (data.shape[:])
Which returns:
(62, 2)
Is there a way to iterate through each column to count [not is.nan]?
If I understand correctly, and you are trying to get the length of non-nan values in each column, use:
np.sum(~np.isnan(data),axis=0)

how to replace elements of pandas series? [duplicate]

This question already has an answer here:
How to split elements of a pandas series and put them into JSON format?
(1 answer)
Closed 6 years ago.
I have a pandas series object S, some elements are name-value pairs, like
a-12
b-23
c-42
d-25
...
some are just
a
b
c
d
....
so on, what I need to do is to get this into Json format like:
{Name:a,Value:12}
{Name:b,Value:23}
{Name:c,Value:42}
{Name:d,Value:25}
...
If only a, b, c, d, not pairs, Values is NaN.
I used str.split("-") function to separate the pairs, for non pairs this would produce NaN for the value part.
I wonder if I can put them together like
result=[{"Name": S.str.split("-").str.get(0),"Value": S.str.split("-").str.get(1)}]
?
I'm not sure you want to start from a series object at all? How did you get there? It might be easier to think of a series as an indexed list, or as a dictionary, in which case you can see that it gets confusing if the items are of different types.
FWIW, you can convert a series directly to json or dict with myseries.to_json() or myseries.to_dict()
Did you try that?

how to combine two numpy arrays and form a new array of a new size [duplicate]

This question already has answers here:
Concatenating two one-dimensional NumPy arrays
(6 answers)
Closed 6 years ago.
I have two numpy arrays that have the same shape(4,1,2).
How can I combine them and get a new array of size(8,1,2) with minimum lines of python code? Not changing values just put them together with A on the top B at the bottom.
A=numpy.array([[[1,1]],
[[2,2]],
[[3,3]],
[[4,4]]]);
B=numpy.array([[[5,5]],
[[6,6]],
[[7,7]],
[[8,8]]]);
numpy.concatenate() should do what you want:
numpy.concatenate((A, B))
Use numpy.vstack()
numpy.vstack([A,B])

Categories

Resources