RedRaven
RedRaven

Reputation: 735

Python Pandas create column based on value of index

I have a dataframe that looks like the below. What I'd like to do is create another column that is based on the VALUE of the index (so anything less the 10 would have another column and be labeled as "small"). I can do something like lengthDF[lengthDF.index < 10] to get the values I want, but I'm sure how to get the additional column I want. I've tried this Create Column with ELIF in Pandas but can't get it to read the index...

             LengthFirst  LengthOthers
0             1           NaN
4           NaN             1
9           NaN             1
13          NaN             1
17            1             1
18          NaN             1
19          NaN             1
20            1           NaN
21            1             1
22            3             4
23            1           NaN
24            7             6
25            1             2
26           16            19
27            1             2
28           24             8
29            9            12
30           73            65
31           15            12
32           55            60
33           28            21
34           29            31

Upvotes: 2

Views: 11227

Answers (1)

capitalistcuttle
capitalistcuttle

Reputation: 1813

Something like this?

lengthDF['size'] = 'large'
lengthDF['size'][lengthDF.index < 10] = 'small'

Upvotes: 4

Related Questions