Pandas split one line to another and output to another line in the same dataframe
For Dataframe, for example:
dt
COL000 COL001
STK_ID
Rowname1 2 2
Rowname2 1 4
Rowname3 1 1
What's the easiest way to add the result of splitting Row1 to Row2 to the same data frame? i.e. the desired output:
COL000 COL001
STK_ID
Rowname1 2 2
Rowname2 1 4
Rowname3 1 1
Newrow 2 0.5
Sorry if this is a simple question, I'm slowly communicating with pandas from the R background.
Thanks in advance!
+3
source to share
2 answers
Below is the code to create a new line with an index d
that is formed from separating the lines a
and b
.
import pandas as pd
df = pd.DataFrame(data={'x':[1,2,3], 'x':[4,5,6]}, index=['a', 'b', 'c'])
df.loc['d'] = df.loc['a'] / df.loc['b']
print(df)
# x y
# a 1.0 4.0
# b 2.0 5.0
# c 3.0 6.0
# d 0.5 0.8
+2
source to share