How do I call a value from a list inside of a pandas dataframe?

rzaratx

I have a some data that I put into a pandas dataframe. Inside of cell [0,5] I have a list of times that I want to call and be printed out.

Dataframe:

GAME_A                                                  PROCESSING_SPEED
yellow_selected                                                       19
red_selected                                                           0
yellow_total                                                          19
red_total                                                             60
counters                [0.849998, 1.066601, 0.883263, 0.91658, 0.96668]

Code:

import pandas as pd

df = pd.read_csv('data.csv', sep = '>')
print(df.iloc[0])
proc_speed = df.iat[0,5]
print(proc_speed[2])

When I try to print the 3rd time in the dictionary I get .. I tried to use a for loop to print the times but instead I get this. How can I call the specific values from the list. How would I print out the 3rd time 0.883263?

[
0
.
8
4
9
9
9
8
,

1
.
0
6
6
...
dhasson

This happens because with the way you are loading the data, the column 'PROCESSING_SPEED' is read as an object type, therefore, all elements of that series are considered strings (i.e., in this case proc_speed = "[0.849998, 1.066601, 0.883263, 0.91658, 0.96668]", which is exactly the string the loop is printing character by character).

Before printing the values you desire to display (from that cell), one should convert the string to a list of numbers, for example:

proc_speed = df.iat[4,1]
proc_speed = [float(s) for s in proc_speed[1:-1].split(',')]
for num in proc_speed:
    print( num)

Where proc_speed[1:-1].split(',') takes the string containing the list, except for the brackets at the beginning and end, and splits it according to the commas separating values.

In general, we have to be careful when loading columns with varying or ambiguous data types, as Pandas could have trouble parsing them correctly or in the way we want/expect it to be.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field?

How do I sort inner list in pandas DataFrame?

How do i change a single index value in pandas dataframe?

How do I filter a pandas DataFrame based on value counts?

How do I set tuple value to pandas dataframe?

pandas/numpy: I have an array with a dictionary inside. How do I create a DataFrame from this?

How can I call for values from pandas dataframe to a function?

How do I access data inside a pandas dataframe groupby object?

pandas - How to extract a value(s) from a list stored in rows of a dataframe

How to remove a value from a list in a Pandas dataframe?

How do I assign each list item to a different pandas dataframe?

How do i convert python list into pandas dataframe with predefined columns

How do I select columns from pandas dataframe that have an average value greater than some limit?

How do I call a function or a method on a select list if the value changes?

How do I extract a word from a pandas dataframe/series using a list in Python?

How do I deconcatenate or unconcatenate a string value in a pandas dataframe?

How do I replace a value in pandas DataFrame based on a condition?

How do I delete [ a list of ] rows from a DataFrame in Pandas?

how do i create a two list from a list inside of an array?

How Do I Call A Function from ViewModel inside bindingHandler

How do I find the position of a value in a pandas.DataFrame?

How do I push coordinates from a pandas dataframe to into a List?

How do I update a specific value inside an Immutable List?

How do I join six list of tuples into a pandas dataframe using the first value of each tuple as the key?

How do I combine data from a Pandas DataFrame with a multiindex into a list

How do I extract a list of lists from a Pandas DataFrame?

How do I assign a value to a random subset of a Pandas DataFrame?

How do I assign a value inside list comprehension

How do I read each of arrays which saved inside list which again is inside column of the pandas dataframe