pandas

How to get the mode of a column in pandas where there are few of the same mode values pandas

試著忘記壹切 提交于 2021-02-05 08:09:31
问题 I have a data frame and i'd like to get the mode of a specific column. i'm using: freq_mode = df.mode()['my_col'][0] However I get the error: ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index my_col') I'm guessing it's because I have few mode that are the same. I need any of the mode, it doesn't matter. How can I use any() to get any of the mode existed? 回答1: For me your code working nice with sample data. If

timedelta64 and datetime conversion

懵懂的女人 提交于 2021-02-05 08:08:37
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

How to flip a column of ratios, convert into a fraction and convert to a float

…衆ロ難τιáo~ 提交于 2021-02-05 08:08:06
问题 I have the following data frame: Date Ratio 0 2000-06-21 4:1 1 2000-06-22 3:2 2 2000-06-23 5:7 3 2000-06-24 7:1 For each item in the Ratio column, I want to reverse the ratio, convert it into a fraction and convert it to a float. Meaning 4:1 would become 1:4, then the : would be replaced with a / and finally it would get 0.25. 3:2 would become 2/3 which is converted to 0.66666666666. So far I only have the following code: df['Ratio'] = df['Ratio'].str.split(":") 回答1: Create new DataFrame with

Arrange two plots horizontally

空扰寡人 提交于 2021-02-05 08:07:28
问题 As an exercise, I'm reproducing a plot from The Economist with matplotlib So far, I can generate a random data and produce two plots independently. I'm struggling now with putting them next to each other horizontally. import pandas as pd import matplotlib.pyplot as plt import numpy as np %matplotlib inline df1 = pd.DataFrame({"broadcast": np.random.randint(110, 150,size=8), "cable": np.random.randint(100, 250, size=8), "streaming" : np.random.randint(10, 50, size=8)}, index=pd.Series(np

timedelta64 and datetime conversion

泄露秘密 提交于 2021-02-05 08:06:47
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

Pandas: How to create a column based on values of another column?

陌路散爱 提交于 2021-02-05 08:06:21
问题 I need to create a new column at the end of a data frame, where the values in that new column are the result of applying some function who's parameters are based on other columns. Specifically, from another column, but a different row. So for example, if my data frame had two columns, containing values x_i , y_i respectively, my third column would be f(x_(i-1), y_(i-1)) I know that to create create a new column, the easiest way would be to do something like df['new_row'] = ... But I'm not

How to flip a column of ratios, convert into a fraction and convert to a float

*爱你&永不变心* 提交于 2021-02-05 08:05:52
问题 I have the following data frame: Date Ratio 0 2000-06-21 4:1 1 2000-06-22 3:2 2 2000-06-23 5:7 3 2000-06-24 7:1 For each item in the Ratio column, I want to reverse the ratio, convert it into a fraction and convert it to a float. Meaning 4:1 would become 1:4, then the : would be replaced with a / and finally it would get 0.25. 3:2 would become 2/3 which is converted to 0.66666666666. So far I only have the following code: df['Ratio'] = df['Ratio'].str.split(":") 回答1: Create new DataFrame with

timedelta64 and datetime conversion

会有一股神秘感。 提交于 2021-02-05 08:05:30
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

How to flip a column of ratios, convert into a fraction and convert to a float

痴心易碎 提交于 2021-02-05 08:04:46
问题 I have the following data frame: Date Ratio 0 2000-06-21 4:1 1 2000-06-22 3:2 2 2000-06-23 5:7 3 2000-06-24 7:1 For each item in the Ratio column, I want to reverse the ratio, convert it into a fraction and convert it to a float. Meaning 4:1 would become 1:4, then the : would be replaced with a / and finally it would get 0.25. 3:2 would become 2/3 which is converted to 0.66666666666. So far I only have the following code: df['Ratio'] = df['Ratio'].str.split(":") 回答1: Create new DataFrame with

Pandas: How to create a column based on values of another column?

ぐ巨炮叔叔 提交于 2021-02-05 08:04:18
问题 I need to create a new column at the end of a data frame, where the values in that new column are the result of applying some function who's parameters are based on other columns. Specifically, from another column, but a different row. So for example, if my data frame had two columns, containing values x_i , y_i respectively, my third column would be f(x_(i-1), y_(i-1)) I know that to create create a new column, the easiest way would be to do something like df['new_row'] = ... But I'm not