This question already has answers here:
dataframe to long format
(2 answers)
Reshape wide to long in pandas
(2 answers)
Split pandas column and add last element to a new column
(2 answers)
Get last "column" after .str.split() operation on column in pandas DataFrame
(5 answers)
Closed 8 months ago.
I have a dataframe like this.
df = pd.DataFrame(np.array([[1, 2, 3, 4], [4, 5, 6, 4], [7, 8, 9, 4]]),
columns=['NP_A', 'NP_B', 'NP_C', "OP_A"])
I would like to use sns.lineplot.
But instead of the name of the columns as 'hue' I would like to make a split of the names.
"NP", "A"
"NP", "B"
...
In order to have a legend that considers the two combinations.
Related
This question already has answers here:
String formatting: Columns in line
(4 answers)
Closed 1 year ago.
i am trying to print out a list of string and integers vertically.
map = [["SG", 8], ["MY", 8], ["PH", 8], ["ID", 8], ["TH", 8]]
the print should return:
SG 8
MY 8
PH 8
ID 8
TH 8
this is what i have :
for element in map:
print(element)
the failed output:
['SG', 8]
['MY', 8]
etc....
May note be the best solution, but try this.
for element in map:
print("{}\t{}".format(element[0],element[1]))
This question already has an answer here:
Cumsum as a new column in an existing Pandas data
(1 answer)
Closed 2 years ago.
Let sat I have a DataFrame with column A.
A= (1,2,3,4,5,6...n)
I want to create column B like this:
B=(1,3,6,10,15,21...n)
Explicitly: i+(sum of all the previous numbers)
Probably simple, but hard for me:P Very new to programming
Thanks!
from itertools import accumulate
A = [1, 2, 3, 4, 5, 6]
B = list(accumulate(A)) #->[1, 3, 6, 10, 15, 21]
This question already has answers here:
Converting pandas dataframe to structured arrays
(4 answers)
Closed 4 years ago.
I want to convert a dataframe:
to array without losing the header column like this:
I tried values:
but have no idea how to keep the column header so I can call it for later use. How can I do it using pandas?
data = data.values
array([[True, 33],
[True, 32],
[True, 31],
...,
[True, 2],
[True, 0],
[True, 0]], dtype=object)
What you're looking for is a way to turn the DataFrame into a structured array, you can find the instructions to do this in the question here Converting pandas dataframe to structured arrays
This question already has answers here:
Translate integers in a numpy array to a contiguous range 0...n
(2 answers)
Closed 4 years ago.
I have a Numpy array with some numbers and I would like to get order the items ascending order.
For example, I have a list:
[4, 25, 100, 4, 50]
And I would like to use a function to get this:
[1, 2, 4, 1, 3]
Any ideas how to do this?
There is a convenient method via pandas:
import pandas as pd
lst = [4, 25, 100, 4, 50]
res = pd.factorize(lst, sort=True)[0] + 1
# [1 2 4 1 3]
This question already has answers here:
Remove duplicate rows of a numpy array [duplicate]
(3 answers)
Closed 6 years ago.
Say I have the following array:
import numpy as np
data = np.array([[51001, 121, 1, 121212],
[51001, 121, 1, 125451],
[51001, 125, 1, 127653]]
I want to remove duplicate rows only by the first 3 elements in a row (first 3 columns).
So the result I will get is:
print data
[[51001, 121, 1, 121212],
[51001, 125, 1, 127653]]
Doesn't matter which row we keep and which row we delete as long as I get the unique by the first 3 columns
Here's one way using drop_duplicates in pandas
In [179]: pd.DataFrame(data).drop_duplicates([0, 1, 2]).values
Out[179]:
array([[ 51001, 121, 1, 121212],
[ 51001, 125, 1, 127653]])