简体   繁体   中英

How can I remove the brackets from a list I'm putting in an appended data frame?

I'm trying to solve the circumference and area of a circle and then create a dataframe that gives me three columns: radius, circumference, area.

The data frame is being created correctly, but it's printing square brackets with it ie "[]"

from math import pi
import pandas as pd

df = []

for r in range(1, 101):

    Values = {'radius': [r],
              'area': [pi * (r * r)],
              'circumference': [2 * pi * r]}

    df.append(Values)


df = pd.DataFrame(df, columns = ['radius', 'circumference', 'area'])

print(df)

The output is ( I'm just showing the first row here. ):

   radius         circumference                  area
0     [1]   [6.283185307179586]   [3.141592653589793]


I'm trying to get the square brackets removed from the output so it looks like:

   radius         circumference                  area
0     1   6.283185307179586   3.141592653589793

I've looked into about a dozen stack overflow questions about this, that all say I should do something like:

df['Values'] = df['Values'].str[0]

That gives me the error "list indices must be integers or slices, not str".

I definitely feel like this is a basic question, but all of the answers say something along the small code directly above this and I can't seem to make sense of it because I don't think I can convert a list to a set of integers?

I tried to convert df = [] to df = str(df)

but it then says I can't append strings.

Any help would be appreciated. Thanks!

from math import pi
import pandas as pd

df = []

for r in range(1, 101):

    Values = {'radius': r,
              'area': pi * (r * r),
              'circumference': 2 * pi * r}

    df.append(Values)


df = pd.DataFrame(df, columns = ['radius', 'circumference', 'area'])

print(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM