pandas

Pandas: Apply function to each pair of columns

百般思念 提交于 2021-02-16 16:11:37
问题 Function f(x,y) that takes two Pandas Series and returns a floating point number. I would like to apply f to each pair of columns in a DataFrame D and construct another DataFrame E of the returned values, so that f(D[i],D[j]) is the value of the i th row and j th column. The straightforward solution is to run a nested loop over all pairs of columns: E = pd.DataFrame([[f(D[i], D[j]) for i in D] for j in D], columns=D.columns, index=D.columns) But is there a more elegant solution that perhaps

Pandas: Apply function to each pair of columns

若如初见. 提交于 2021-02-16 16:10:57
问题 Function f(x,y) that takes two Pandas Series and returns a floating point number. I would like to apply f to each pair of columns in a DataFrame D and construct another DataFrame E of the returned values, so that f(D[i],D[j]) is the value of the i th row and j th column. The straightforward solution is to run a nested loop over all pairs of columns: E = pd.DataFrame([[f(D[i], D[j]) for i in D] for j in D], columns=D.columns, index=D.columns) But is there a more elegant solution that perhaps

Extract value of a particular column name in pandas as listed in another column

谁说胖子不能爱 提交于 2021-02-16 15:09:30
问题 The title wasn't too clear but here's an example. Suppose I have: person apple orange type Alice 11 23 apple Bob 14 20 orange and I want to get this column person new_col Alice 11 Bob 20 so we get the column 'apple' for row 'Alice' and 'orange' for row 'Bob'. I'm thinking iterrows, but that would be slow. Are there faster ways to do this? 回答1: Use DataFrame.lookup: df['new_col'] = df.lookup(df.index, df['type']) print (df) person apple orange type new_col 0 Alice 11 23 apple 11 1 Bob 14 20

Extract value of a particular column name in pandas as listed in another column

久未见 提交于 2021-02-16 15:09:28
问题 The title wasn't too clear but here's an example. Suppose I have: person apple orange type Alice 11 23 apple Bob 14 20 orange and I want to get this column person new_col Alice 11 Bob 20 so we get the column 'apple' for row 'Alice' and 'orange' for row 'Bob'. I'm thinking iterrows, but that would be slow. Are there faster ways to do this? 回答1: Use DataFrame.lookup: df['new_col'] = df.lookup(df.index, df['type']) print (df) person apple orange type new_col 0 Alice 11 23 apple 11 1 Bob 14 20

Getting index name for the max value in DF [duplicate]

↘锁芯ラ 提交于 2021-02-16 15:09:26
问题 This question already has answers here : Pandas max value index (3 answers) Closed 2 years ago . I have the following dataframe: data = {'Algorithm': ['KNN', 'Decision Tree', 'SVM', 'Logistic Regression'], 'Jaccard': [0.75,0.65,0.67,0.70], 'F1-score': [0.69,0.78, 0.75, 0.77], 'LogLoss': ['NA', 'NA', 'NA', 5.23]} report= pd.DataFrame(data = data) report = report[['Algorithm', 'Jaccard', 'F1-score', 'LogLoss']] report.set_index(report['Algorithm'], inplace = True) report.drop(columns = [

Extract value of a particular column name in pandas as listed in another column

人盡茶涼 提交于 2021-02-16 15:08:57
问题 The title wasn't too clear but here's an example. Suppose I have: person apple orange type Alice 11 23 apple Bob 14 20 orange and I want to get this column person new_col Alice 11 Bob 20 so we get the column 'apple' for row 'Alice' and 'orange' for row 'Bob'. I'm thinking iterrows, but that would be slow. Are there faster ways to do this? 回答1: Use DataFrame.lookup: df['new_col'] = df.lookup(df.index, df['type']) print (df) person apple orange type new_col 0 Alice 11 23 apple 11 1 Bob 14 20

Extract value of a particular column name in pandas as listed in another column

て烟熏妆下的殇ゞ 提交于 2021-02-16 15:08:22
问题 The title wasn't too clear but here's an example. Suppose I have: person apple orange type Alice 11 23 apple Bob 14 20 orange and I want to get this column person new_col Alice 11 Bob 20 so we get the column 'apple' for row 'Alice' and 'orange' for row 'Bob'. I'm thinking iterrows, but that would be slow. Are there faster ways to do this? 回答1: Use DataFrame.lookup: df['new_col'] = df.lookup(df.index, df['type']) print (df) person apple orange type new_col 0 Alice 11 23 apple 11 1 Bob 14 20

Save data from TWS API to csv file

本秂侑毒 提交于 2021-02-16 15:03:13
问题 I have a python script that reads data from the TWS API (Interactive Brokers) and want to dump the data in a csv file. Right now it just overwrites the data and prints the last line along with a bunch of other values I don't want. It prints out the values fine with print(df). Code: from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.common import * from ibapi.contract import * from threading import Timer from ibapi.ticktype import * import pandas as pd import numpy

Pandas - groupby all columns and mark in original dataframe [duplicate]

馋奶兔 提交于 2021-02-16 14:53:57
问题 This question already has answers here : Pandas: assign an index to each group identified by groupby (6 answers) Closed 2 years ago . I have a DataFrame with columns 'Id' which is unique, and 'A', 'B', 'C' , etc... There are different rows where all values 'A', 'B', 'C' are the same. I'd like to give them a group name (a running index from 1). For example: df = pd.DataFrame({"A": [1, 1, 1, 2], "B": [3, 4, 4, 4], "C": [5, 5, 5, 5]}) df Out[127]: A B C 0 1 3 5 1 1 4 5 2 1 4 5 3 2 4 5 Will

Pandas - groupby all columns and mark in original dataframe [duplicate]

半城伤御伤魂 提交于 2021-02-16 14:53:01
问题 This question already has answers here : Pandas: assign an index to each group identified by groupby (6 answers) Closed 2 years ago . I have a DataFrame with columns 'Id' which is unique, and 'A', 'B', 'C' , etc... There are different rows where all values 'A', 'B', 'C' are the same. I'd like to give them a group name (a running index from 1). For example: df = pd.DataFrame({"A": [1, 1, 1, 2], "B": [3, 4, 4, 4], "C": [5, 5, 5, 5]}) df Out[127]: A B C 0 1 3 5 1 1 4 5 2 1 4 5 3 2 4 5 Will