logo
down
shadow

Pandas DataFrame Assignment Issue - Possible Bug?


Pandas DataFrame Assignment Issue - Possible Bug?

By : Mahandra
Date : November 22 2020, 10:33 AM
I wish this help you I need to upgrade to Pandas 0.14.0, according to jreback on github:
https://github.com/pydata/pandas/issues/9200
code :


Share : facebook icon twitter icon
Pandas Dataframe using .loc for assignment gives unexpected results

Pandas Dataframe using .loc for assignment gives unexpected results


By : Richard Xuereb
Date : March 29 2020, 07:55 AM
hop of those help? I'm assuming it has something to do with views/copies, but it certainly seems like unexpected behavior - you might open an issue on github.
https://github.com/pydata/pandas/issues
code :
In [86]: import numpy as np
In [87]: df['value/unit'] = np.where(df['units'] == 0, df['prev value/unit'], df['value']/df['units'])

In [88]: df
Out[87]: 
         prev value/unit  value  units  value/unit
series1               99    100    100           1
series2               99    100    100           1
series3               99    100    100           1
pandas multiindex assignment from another dataframe

pandas multiindex assignment from another dataframe


By : user3157160
Date : March 29 2020, 07:55 AM
Does that help I am trying to understand pandas MultiIndex DataFrames and how to assign data to them. Specifically I'm interested in assigning entire blocks that match another smaller data frame. , When you use
code :
df.loc['A', :] = df_
ix_ = pd.MultiIndex.from_product([['A'], ['a', 'b', 'c', 'd']])
df_.index = ix_
df.loc['A', :] = df_
print(df)
A a  0.229970  0.730824  0.784356
  b  0.584390  0.628337  0.318222
  c  0.257192  0.624273  0.221279
  d  0.787023  0.056342  0.240735
B a       NaN       NaN       NaN
  b       NaN       NaN       NaN
  c       NaN       NaN       NaN
  d       NaN       NaN       NaN
df.loc['A', :] = df_.values
idx = pd.IndexSlice
df.loc[idx[:,('a','b')], :] = df_.values
In [85]: df
Out[85]: 
          1st       2nd       3rd
A a  0.229970  0.730824  0.784356
  b  0.584390  0.628337  0.318222
  c       NaN       NaN       NaN
  d       NaN       NaN       NaN
B a  0.257192  0.624273  0.221279
  b  0.787023  0.056342  0.240735
  c       NaN       NaN       NaN
  d       NaN       NaN       NaN
Python Pandas Dataframe assignment

Python Pandas Dataframe assignment


By : Henrique Vaz
Date : March 29 2020, 07:55 AM
help you fix your problem The root cause of this error message is the categorical nature of the month column:
code :
In [42]: flights.dtypes
Out[42]:
year             int64
month         category
passengers       int64
dtype: object

 In [43]: flights.month.cat.categories
Out[43]: Index(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], d
type='object')
In [45]: flights.month.cat.add_categories('total', inplace=True)

In [46]: x = flights.pivot(index='year', columns='month', values='passengers')

In [47]: x['total'] = x.sum(1)

In [48]: x
Out[48]:
month  January  February  March  April    May   June   July  August  September  October  November  December   total
year
1949     112.0     118.0  132.0  129.0  121.0  135.0  148.0   148.0      136.0    119.0     104.0     118.0  1520.0
1950     115.0     126.0  141.0  135.0  125.0  149.0  170.0   170.0      158.0    133.0     114.0     140.0  1676.0
1951     145.0     150.0  178.0  163.0  172.0  178.0  199.0   199.0      184.0    162.0     146.0     166.0  2042.0
1952     171.0     180.0  193.0  181.0  183.0  218.0  230.0   242.0      209.0    191.0     172.0     194.0  2364.0
1953     196.0     196.0  236.0  235.0  229.0  243.0  264.0   272.0      237.0    211.0     180.0     201.0  2700.0
1954     204.0     188.0  235.0  227.0  234.0  264.0  302.0   293.0      259.0    229.0     203.0     229.0  2867.0
1955     242.0     233.0  267.0  269.0  270.0  315.0  364.0   347.0      312.0    274.0     237.0     278.0  3408.0
1956     284.0     277.0  317.0  313.0  318.0  374.0  413.0   405.0      355.0    306.0     271.0     306.0  3939.0
1957     315.0     301.0  356.0  348.0  355.0  422.0  465.0   467.0      404.0    347.0     305.0     336.0  4421.0
1958     340.0     318.0  362.0  348.0  363.0  435.0  491.0   505.0      404.0    359.0     310.0     337.0  4572.0
1959     360.0     342.0  406.0  396.0  420.0  472.0  548.0   559.0      463.0    407.0     362.0     405.0  5140.0
1960     417.0     391.0  419.0  461.0  472.0  535.0  622.0   606.0      508.0    461.0     390.0     432.0  5714.0
In [76]: flights_unstacked.columns = \
    ...:     flights_unstacked.columns \
    ...:     .set_levels(flights_unstacked.columns.get_level_values(1).categories,
    ...:                 level=1)
    ...:

In [77]: flights_unstacked['passengers','total']  = flights_unstacked.sum(axis=1)

In [78]: flights_unstacked
Out[78]:
      passengers
month    January February March April  May June July August September October November December total
year
1949         112      118   132   129  121  135  148    148       136     119      104      118  1520
1950         115      126   141   135  125  149  170    170       158     133      114      140  1676
1951         145      150   178   163  172  178  199    199       184     162      146      166  2042
1952         171      180   193   181  183  218  230    242       209     191      172      194  2364
1953         196      196   236   235  229  243  264    272       237     211      180      201  2700
1954         204      188   235   227  234  264  302    293       259     229      203      229  2867
1955         242      233   267   269  270  315  364    347       312     274      237      278  3408
1956         284      277   317   313  318  374  413    405       355     306      271      306  3939
1957         315      301   356   348  355  422  465    467       404     347      305      336  4421
1958         340      318   362   348  363  435  491    505       404     359      310      337  4572
1959         360      342   406   396  420  472  548    559       463     407      362      405  5140
1960         417      391   419   461  472  535  622    606       508     461      390      432  5714
Pandas Nested DataFrame assignment

Pandas Nested DataFrame assignment


By : Coleman Haley
Date : March 29 2020, 07:55 AM
hope this fix your issue I have the following DataFrame: , This line
code :
    df["operator_proba"]["country_name"] = country_name["mno_subscribers"] / mno_sum
df['operator_proba'] = df.groupby('country_name')['mno_subscribers'].apply(lambda x : x/x.sum())
   prefix operator_name country_name  mno_subscribers  operator_proba
0   267.0        Airtel     Botswana              490        1.000000
1   373.0        Orange      Moldova              207        1.000000
2   248.0        Airtel   Seychelles              490        1.000000
3    91.0      Reliance     Bostwana               92        0.151316
4   233.0      Vodafone     Bostwana              516        0.848684
Pandas Dataframe to Dataframe Assignment Not Aligning and Producing NaN

Pandas Dataframe to Dataframe Assignment Not Aligning and Producing NaN


By : blackpit
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to assign the values of one Pandas dataframe to another dataframe. However, the assignment results are not behaving as I expected and I'm not sure why. I have a workaround, however, I don't understand why this workaround is necessary or whether it is a preferred workaround. , Explaining @coldspeed comment.
Try this:
code :
df1.loc[bad, 'col2'] 
1    ERROR
2    ERROR
3    ERROR
Name: col2, dtype: object
    col3
0   b
1   c
2   d
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org