Reputation: 724
I have a dataframe df=pd.DataFrame({'xx':[['100','5','3'], ['5','40'], ['100']]})
and I would like to have the maximum of each list as a number. So I would like to get this:
xx
0 100
1 40
2 100
Is there a way to do this?
Upvotes: 1
Views: 2773
Reputation: 1
import pandas
data = pandas.read_csv("weather_data.csv")
data_list = data["temp"].max()
print(data_list)
in the file of weather_data.csv we have a list which we called temp if we need temp list maixmum value we can apply this method
Upvotes: 0
Reputation: 17322
you can use pandas.Series.apply:
df['max_xx'] = df['xx'].apply(lambda x: max(map(int, x)))
df['max_xx']
output:
0 100
1 40
2 100
if you want to use pandas.DataFrame.assign you can use:
df.assign(max_xx=lambda x: [max(map(int, l)) for l in x.xx])
Upvotes: 0
Reputation: 75080
Adding another pandas method using series.explode
to explode the series of lists into 1 series , then using series.astype
convert to int , then take max grouped by index:
df['max_col'] = df['xx'].explode().astype(int).max(level=0)
#or:-> df['xx'].explode().astype(int).groupby(level=0).max()
0 100
1 40
2 100
Upvotes: 1
Reputation: 862831
Convert values to integers and get max
value:
df['xx'] = df['xx'].map(lambda x: max(int(y) for y in x))
Or use list comprehension:
df['xx'] = [max(int(y) for y in x) for x in df['xx']]
print(df)
xx
0 100
1 40
2 100
Upvotes: 1