I have a dataframe whose columns are numeric indexes, which aren't necessarily contiguous. I want to add a new column to it with a particular index, similar to:
df[4] = [1,2,3,4]
But without modifying the existing dataframe. df.assign only accepts kwargs (it can't be directly passed an actual dictionary), and even the (rather kludgy anyway) method of expanding a non-str-keyed dict as kwargs is explicitly guarded against:
>>> df.assign(**{4: [1,2,3,4]})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: assign() keywords must be strings
Using pd.concat works, but has a lot of line noise:
>>> a
4 0 1 2 3
0 1 1 2 3 4
1 2 2 3 5 4
>>> pd.concat([a, pd.DataFrame({6: [1,2]})], axis=1)
4 0 1 2 3 6
0 1 1 2 3 4 1
1 2 2 3 5 4 2
Is there a nicer way?
_ = pd.concat(...)and get rid of it?