我正在尝试计算我称之为“delta值”的内容,这意味着两个连续行之间的数量已经发生了变化。
例如
A | delta_A
1 | 0
2 | 1
5 | 3
9 | 4
我设法从这段代码开始(基本上是从我的MatLab程序中复制而来)
df = df.assign(delta_A=np.zeros(len(df.A)))
df['delta_A'][0] = 0 # start at 'no-change'
df['delta_A'][1:] = df.A[1:].values - df.A[:-1].values
正确生成数据框,似乎没有进一步的负面影响
但是,我觉得这种方法有问题,因为我收到这些消息。
正在尝试在DataFrame
的切片副本上设置值请参阅文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
.../__main__.py:5: SettingWithCopyWarning
所以,我真的不明白那个链接试图说什么,我找到了这篇文章
Adding new column to existing DataFrame in Python pandas
而且,作为答案的最新编辑说有使用此代码,但我已经使用了这种语法......
df1 = df1.assign(e=p.Series(np.random.randn(sLength)).values)
所以,问题是 - loc()
函数是否可行,或者获取该列的更正确方法是什么?
答案 0 :(得分:2)
您似乎需要diff
,然后将public partial class LanguageSettings : PhoneApplicationPage
{
public LanguageSettings()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (ChangeLanguageCombo.Items.Count == 0)
{ ChangeLanguageCombo.Items.Add(LocalizationManager.SupportedLanguages.En);
ChangeLanguageCombo.Items.Add(LocalizationManager.SupportedLanguages.Bn);
}
SelectChoice();
}
private void ButtonSaveLang_OnClick(object sender, RoutedEventArgs e)
{
//Store the Messagebox result in result variable
MessageBoxResult result = MessageBox.Show("App language will be changed. Do you want to continue?", "Apply Changes", MessageBoxButton.OKCancel);
//check if user clicked on ok
if (result == MessageBoxResult.OK)
{
var languageComboBox = ChangeLanguageCombo.SelectedItem;
LocalizationManager.ChangeAppLanguage(languageComboBox.ToString());
//Application.Current.Terminate(); I am commenting out because I don't neede to restart my app anymore.
}
else
{
SelectChoice();
}
}
private void SelectChoice()
{
//Select the saved language
string lang = LocalizationManager.GetCurrentAppLang();
if(lang == "bn-BD")
ChangeLanguageCombo.SelectedItem = ChangeLanguageCombo.Items[1];
else
{
ChangeLanguageCombo.SelectedItem = ChangeLanguageCombo.Items[0];
}
}
}
替换为NaN
:
0
使用assign
df['delta_A'] = df.A.diff().fillna(0).astype(int)
A delta_A
0 0 0
1 4 4
2 7 3
3 8 1
另一种解决方案,如果您只需要替换第一个df = df.assign(delta_A=df.A.diff().fillna(0).astype(int))
A delta_A
0 0 0
1 4 4
2 7 3
3 8 1
值:
NaN
您的解决方案可以使用iloc
修改,但我认为最好使用df['delta_A'] = df.A.diff()
df.loc[df.index[0], 'delta_A'] = 0
print (df)
A delta_A
0 0 0.0
1 4 4.0
2 7 3.0
3 8 1.0
功能:
diff