T
T
Timebird2018-07-12 18:18:29
Python
Timebird, 2018-07-12 18:18:29

How to create a new column using other columns with a priori content constraints?

There is a dataframe, in the first column the data can be both int and str.

Col1       Col2       Col3
автобус      4       100500
37           2       100500
57           3       100500
троллейбус   4       100500

Required: create a new column to be like the first one, but only if the corresponding row data of the first column is of type str
Col1       Col2       Col3        Col4
автобус      4       100500       автобус
37           2       100500       NaN
57           3       100500       NaN
троллейбус   4       100500       троллейбус

How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-07-19
@Trif

not numpy, of course, but with a regular two-dimensional array, I would do this:

base = [
    ['автобус', 4, 100500],
    [37, 2, 100500],
    [57, 3, 100500],
    ['троллейбус', 4, 100500]
    ]

for i in base:
    i.append(i[0] if isinstance(i[0], str) else 'NaN')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question