T
T
Timebird2018-04-09 19:04:47
Python
Timebird, 2018-04-09 19:04:47

How to iterate over a dataframe?

There is a dataframe data(in the picture).
I want to: replace type {} in the values ​​of each column with just {}. For example, type 5 by 5.
As planned to implement: make a generator that runs through the values ​​of each column and leaves only a number for split.
Implementation:

columns_names = data.columns

def columns_gen(data, column_name):
    for value in data[column_name].values:
        if type(value) == str:
            value = value.split()[1]
            yield value

for column_name in columns_names:
    columns_gen(data, column_name)

5acb8f0141b59455989024.png
But nothing changes, the dataframe remains the same as in the picture. Why?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2018-04-09
@assembled

Try:

for column_name in data.colums:
    for i in len ( data.values ) :
        if type ( data [ column_name ] .values [ i ] == str :
            data [ column_name ] .values [ i ] = data [ column_name ] .values [ i ] .split() [ 1 ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question