Here are 2 data frames that I would like to be concat together column wise.
>>> df1 = pd.DataFrame({'letters' : ['a', 'b', 'c'], 'numbers' : [1, 2, 3]})
>>> df2 = pd.DataFrame({'Cities' : ['Rome', 'Venice'], 'floats' : [1.1 , 2.2]})
>>> df1
letters numbers
0 a 1
1 b 2
2 c 3
>>> df2
Cities floats
0 Rome 1.1
1 Venice 2.2
There is a mismatch in the number of rows. I would like to append a copy of the second row (could be any arbitrary row) so I tried this...
>>> df2.ix[[0, 1, 1]]
Cities floats
0 Rome 1.1
1 Venice 2.2
1 Venice 2.2
When concating the 2 data frames I get a ValueError...
pd.concat([df1, df2.ix[[0, 1, 1]]], axis = 1)
ValueError: Shape of passed values is (4, 6), indices imply (4, 4)
I tried making a new copy of the table with the replicated row with no avail...
pd.concat([df1, df2.ix[[0, 1, 1]]].copy(), axis = 1)
ValueError: Shape of passed values is (4, 6), indices imply (4, 4)
This is more of a contrived example for understanding than an actual problem as the premise is a little silly. I would still like a proper answer though with an expected result of...
letters numbers Cities floats
0 a 1 Rome 1.1
1 b 2 Venice 2.2
2 c 3 Venice 2.2
pd.concat
aligns rows based on the index of the DataFrames. Since df2.ix[...]
has two rows with the same index, pd.concat
does not place the second "Venice" row on a row with index 2. To renumber the index, call reset_index()
before concat'ing:
In [102]: pd.concat([df1, df2.iloc[[0, 1, 1]].reset_index()], axis=1)
Out[102]:
letters numbers index Cities floats
0 a 1 0 Rome 1.1
1 b 2 1 Venice 2.2
2 c 3 1 Venice 2.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.