dataframe

R - Add columns to dataframes in list by looping through elements in a vector

谁说胖子不能爱 提交于 2021-02-19 04:14:54
问题 I am working with several datasets that measure the same variables over many years. I am trying to add a year variable to each dataset, but more generally I want to loop through elements in a vector and add each as a new column in a list of dataframes. This question was similar to mine but I want to iteratively add each element in a vector to the corresponding dataframe as a new column: R - New variables over several data frames in a loop Here's sample data: year <- c(1:3) data1 <- data.frame

R - Add columns to dataframes in list by looping through elements in a vector

拟墨画扇 提交于 2021-02-19 04:13:50
问题 I am working with several datasets that measure the same variables over many years. I am trying to add a year variable to each dataset, but more generally I want to loop through elements in a vector and add each as a new column in a list of dataframes. This question was similar to mine but I want to iteratively add each element in a vector to the corresponding dataframe as a new column: R - New variables over several data frames in a loop Here's sample data: year <- c(1:3) data1 <- data.frame

Faking whether an object is an Instance of a Class in Python

浪尽此生 提交于 2021-02-19 03:57:07
问题 Suppose I have a class FakePerson which imitates all the attributes and functionality of a base class RealPerson without extending it . In Python 3, is it possible to fake isinstance() in order to recognise FakePerson as a RealPerson object by only modifying the FakePerson class. For example: class RealPerson(): def __init__(self, age): self.age = age def are_you_real(self): return 'Yes, I can confirm I am a real person' def do_something(self): return 'I did something' # Complicated

How to pivot pandas DataFrame column to create binary “value table”?

狂风中的少年 提交于 2021-02-19 03:37:46
问题 I have the following pandas dataframe: import pandas as pd df = pd.read_csv("filename.csv") df A B C D E 0 a 0.469112 -0.282863 -1.509059 cat 1 c -1.135632 1.212112 -0.173215 dog 2 e 0.119209 -1.044236 -0.861849 dog 3 f -2.104569 -0.494929 1.071804 bird 4 g -2.224569 -0.724929 2.234213 elephant ... I would like to create more columns based on the identity of categorical values in column E such that the dataframe looks like this: df A B C D cat dog bird elephant .... 0 a 0.469112 -0.282863 -1

Assigning a scalar value to an empty DataFrame doesn't appear to do anything

大憨熊 提交于 2021-02-19 03:24:58
问题 I'm new to pandas and have a very basic question, please! On Python v3.6 through spyder: x= pd.DataFrame(columns = ['1','2']) print(x) x['1'] = '25' print(x) From the print statements, the dataframe x does not appear to change. My question: What does x['1'] = '25' do, if anything? 回答1: There is actually a difference between the semantics of assigning scalars and iterables (think containers such as lists as list-like objects). Consider, df = pd.DataFrame(columns=['1', '2']) df Empty DataFrame

Forward fill column with an index-based limit

倾然丶 夕夏残阳落幕 提交于 2021-02-19 02:55:07
问题 I want to forward fill a column and I want to specify a limit, but I want the limit to be based on the index---not a simple number of rows like limit allows. For example, say I have the dataframe given by: df = pd.DataFrame({ 'data': [0.0, 1.0, np.nan, 3.0, np.nan, 5.0, np.nan, np.nan, np.nan, np.nan], 'group': [0, 0, 0, 1, 1, 0, 0, 0, 1, 1] }) which looks like In [27]: df Out[27]: data group 0 0.0 0 1 1.0 0 2 NaN 0 3 3.0 1 4 NaN 1 5 5.0 0 6 NaN 0 7 NaN 0 8 NaN 1 9 NaN 1 If I group by the

how to get a continuous rolling mean in pandas?

一曲冷凌霜 提交于 2021-02-19 02:46:39
问题 Looking to get a continuous rolling mean of a dataframe. df looks like this index price 0 4 1 6 2 10 3 12 looking to get a continuous rolling of price the goal is to have it look this a moving mean of all the prices. index price mean 0 4 4 1 6 5 2 10 6.67 3 12 8 thank you in advance! 回答1: you can use expanding: df['mean'] = df.price.expanding().mean() df index price mean 0 4 4.000000 1 6 5.000000 2 10 6.666667 3 12 8.000000 回答2: Welcome to SO: Hopefully people will soon remember you from

Convert Bigquery results to Pandas Data Frame

末鹿安然 提交于 2021-02-19 02:38:02
问题 Below is the code to convert BigQuery results into Pandas data frame. Im learning Python&Pandas and wonder if i can get suggestion/ideas about any kind of improvements to the code? #...code to run query, that returns 3 columns: 'date' DATE, 'currency' STRING,'rate' FLOAT... rows, total_count, token = query.fetch_data() currency = [] rate = [] dates = [] for row in rows: dates.append(row[0]) currency.append(row[1]) rate.append(row[2]) dict = { 'currency' : currency, 'date' : dates, 'rate' :

How to do pandas equivalence of SQL outer join without a key

断了今生、忘了曾经 提交于 2021-02-18 22:46:47
问题 In SQL, you can join two tables without a key so that all records of both tables merge with each other. If pandas.concat() or pandas.merge() or some other pandas syntax supported this, it could help me with one step of a problem I am trying to solve. I found an outer join option on the help documentation, but I could not find an exact syntax to do what I wanted (join all records without a key). To explain this a little better: import pandas as pd lunchmenupairs2 = [["pizza", "italian"],[

How to do pandas equivalence of SQL outer join without a key

拥有回忆 提交于 2021-02-18 22:44:51
问题 In SQL, you can join two tables without a key so that all records of both tables merge with each other. If pandas.concat() or pandas.merge() or some other pandas syntax supported this, it could help me with one step of a problem I am trying to solve. I found an outer join option on the help documentation, but I could not find an exact syntax to do what I wanted (join all records without a key). To explain this a little better: import pandas as pd lunchmenupairs2 = [["pizza", "italian"],[