Reputation: 793
I have data frames with same column names so i have merge them
wave num stlines
0 4050.32 3.0 0.282690
1 4208.98 5.5 0.490580
2 4374.94 9.0 0.714830
3 4379.74 9.0 0.314040
4 4398.01 14.0 0.504150
5 4502.21 8.0 0.562780
wave num stlines
0 4050.32 3 0.28616
1 4208.98 6 0.48781
2 4374.94 9 0.71548
3 4379.74 10 0.31338
4 4398.01 15 0.49950
5 4502.21 9 0.56362
wave num stlines
0 4050.32 3.0 0.282690
1 4208.98 7.5 0.490580
2 4374.94 9.0 0.714830
3 4379.74 9.0 0.314040
4 4398.01 14.0 0.504150
5 4502.21 8.0 0.562780
after merging, the resultant dataframe looks like this:
df=pd.merge(df1,df2,df3, on='wave',axis=1,join='inner')
wave num_x stlines_x num_x stlines_x num_x stlines_x
0 4050.32 3.0 0.282690 3 0.28616 3.0 0.282690
1 4208.98 5.5 0.490580 6 0.48781 5.5 0.490580
2 4374.94 9.0 0.714830 9 0.71548 9.0 0.714830
3 4379.74 9.0 0.314040 10 0.31338 9.0 0.314040
4 4398.01 14.0 0.504150 15 0.49950 14.0 0.504150
5 4502.21 8.0 0.562780 9 0.56362 8.0 0.562780
So now if i want to take the values of all the coulmns with namenum_x
for any row. Then how can i get them?
I can get the complete columns with same name using the following
df.num_x
num num num
0 3 3.0 3
1 5.5 6 7.5
2 9 9.0 9
3 10 14.0 10
4 15 8.0 15
5 9 3.0 9
but when i tried to do the same for a single row '1' then it didn't work:
df.num_x['1']
The desired result should look like this:
num num num
1 5.5 6 7.5
How can i get them??
Upvotes: 1
Views: 35
Reputation: 863166
You need DataFrame.loc
:
df.loc[1, 'num_x']
In pandas same columns names are problematic, because not easy seelct first, second num_x
, so suggest create MultiIndex
:
dfs = [df1, df2, df3]
df = pd.concat([x.set_index('wave') for x in dfs],
axis=1,
keys=['df1','df2','df3'], join='inner')
print (df)
df1 df2 df3
num stlines num stlines num stlines
wave
4050.32 3.0 0.28269 3 0.28616 3.0 0.28269
4208.98 5.5 0.49058 6 0.48781 7.5 0.49058
4374.94 9.0 0.71483 9 0.71548 9.0 0.71483
4379.74 9.0 0.31404 10 0.31338 9.0 0.31404
4398.01 14.0 0.50415 15 0.49950 14.0 0.50415
4502.21 8.0 0.56278 9 0.56362 8.0 0.56278
And then use xs
for selecting:
df1 = df.xs('num', axis=1, level=1)
print (df1)
df1 df2 df3
wave
4050.32 3.0 3 3.0
4208.98 5.5 6 7.5
4374.94 9.0 9 9.0
4379.74 9.0 10 9.0
4398.01 14.0 15 14.0
4502.21 8.0 9 8.0
Upvotes: 1